2

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());
peterh
  • 11,875
  • 18
  • 85
  • 108
Twisterz
  • 424
  • 4
  • 15
  • 31
  • It's probably not working because you're using the wrong version of Java for that to work. The question you linked to is using a `com.sun.*` class, which is [strongly discouraged](http://stackoverflow.com/questions/1834826/it-is-a-bad-practice-to-use-suns-proprietary-java-classes) for this very reason, btw. As for how to fix it, I'm not entirely sure, sorry. – Brian Oct 23 '12 at 19:56
  • Still a good question though, so +1 :) – Brian Oct 23 '12 at 19:57
  • the answer I was referring to doesn't use the com.sun.* it literally changes the trust manager. That was the answer I was talking about, because i did read up on how bad using com.sun is. – Twisterz Oct 23 '12 at 19:59
  • I haven't tried it, but what type of errors do you get? – siegi Oct 23 '12 at 20:02
  • 1
    it mainly has a huge issue with this line of code: `sc.init(null, new TrustManager[] { trm }, null);` – Twisterz Oct 23 '12 at 20:03
  • Is it a compilation error or a runtime error? – uldall Oct 23 '12 at 20:06
  • 1
    Can you please paste the error in the original question? – uldall Oct 23 '12 at 20:09
  • 1
    Ah, okay. Remember that you can get the link to an answer by using the `share` link underneath it, that way you can link directly to the answer so we know which answer you're referring to, otherwise we may assume it's the accepted answer. – Brian Oct 23 '12 at 20:23
  • Ah thank you, that was a good tip. I will update the question now. – Twisterz Oct 23 '12 at 20:25

1 Answers1

1

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.

siegi
  • 5,646
  • 2
  • 30
  • 42
  • After I tried this I am still left with two errors: `SecurityCheck.java:28: error: unreported exception NoSuchAlgorithmException; must be caught or declared to be thrown SSLContext sc = SSLContext.getInstance("SSL");` `SecurityCheck.java:29: error: unreported exception KeyManagementException; must be caught or declared to be thrown sc.init(null, new TrustManager[] { trm }, null);` – Twisterz Oct 24 '12 at 03:16
  • that should work, but for some reason I'm getting errors saying that it can't find the symbol "class NoSuchAlgorithmException" and "class KeyManagementException", I feel stupid not being able to solve this on my own, I just must be missing something. – Twisterz Oct 24 '12 at 13:45
  • @Twisterz I think you need to add the exception classes to the imports, i.e. `import java.security.NoSuchAlgorithmException` and `import java.security.KeyManagementException` or simply `import java.security.*` to import all classes in the `java.security` package. – siegi Oct 24 '12 at 14:56
  • Yeah I just figured that out haha. I feel dumb just like I thought I would. – Twisterz Oct 24 '12 at 15:06