Question

After visiting this question.

I saw a few answers and the best was obviously the answer by so_mv. Now it seems that his answer is now outdated, because I have tried it with all the imports and exact code, but it generates tons of errors. I've looked into the documentation to see if anything has changed in the most recent java, but I can't seem to find what the cause is. I think an updated answer to that question would not only benefit me, but the community as a whole.

Errors:

SecurityCheck.java:28: error: <identifier> expected
        sc.init(null, new TrustManager[] { trm }, null);
               ^
SecurityCheck.java:28: error: illegal start of type
        sc.init(null, new TrustManager[] { trm }, null);
                ^
SecurityCheck.java:28: error: illegal start of type
        sc.init(null, new TrustManager[] { trm }, null);
                      ^
SecurityCheck.java:28: error: ')' expected
        sc.init(null, new TrustManager[] { trm }, null);
                         ^
SecurityCheck.java:28: error: not a statement
        sc.init(null, new TrustManager[] { trm }, null);
                                           ^
SecurityCheck.java:28: error: ';' expected
        sc.init(null, new TrustManager[] { trm }, null);
                                              ^
SecurityCheck.java:28: error: illegal start of type
        sc.init(null, new TrustManager[] { trm }, null);
                                                ^
SecurityCheck.java:28: error: ';' expected
        sc.init(null, new TrustManager[] { trm }, null);
                                                 ^
SecurityCheck.java:28: error: illegal start of type
        sc.init(null, new TrustManager[] { trm }, null);
                                                      ^
SecurityCheck.java:28: error: <identifier> expected
        sc.init(null, new TrustManager[] { trm }, null);
                                                       ^
SecurityCheck.java:28: error: ';' expected
        sc.init(null, new TrustManager[] { trm }, null);
                                                        ^
SecurityCheck.java:29: error: illegal start of type
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                          ^
SecurityCheck.java:29: error: <identifier> expected
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                                                                         ^
SecurityCheck.java:29: error: ';' expected
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                                                                          ^
SecurityCheck.java:29: error: illegal start of type
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                                                                           ^
SecurityCheck.java:29: error: <identifier> expected
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                                                                            ^
SecurityCheck.java:29: error: ';' expected
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
Was it helpful?

Solution

After looking at your errors I think the problem is you have put the code directly in the class block, but it is supposed to be put in a method like this:

// package here
// imports here
public class SecurityCheck
{
    public void test() throws NoSuchAlgorithmException, KeyManagementException
                              // and any other exception here
    {
        // alternatively to throwing the exceptions to the caller,
        // you can handle them here using a try-catch-block

        // code from answer you linked to here
    }
}

I have not tried it and I don't know if this are the only problems, but it would explain the errors you get. The first lines are variable declarations and initializations valid in a class body, but the line sc.init(null, new TrustManager[] { trm }, null); is not (as it is a statement) and needs to be in a method. This is also the reason the errors start at this line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top