简体   繁体   中英

Glassfish 5.0 java.lang.NoSuchMethodError

When i tried connect google custom search api glassfish throw nosuchmethoderror

That's codes

private int conn (String search)throws Exception {

    String key="mykey";
    URL url = new URL("https://www.googleapis.com/customsearch/v1?q="+search+"&cx=017576662512468239146%3Aomuauf_lfve&key="+key);
    doTrustToCertificates();
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setDoOutput(true);
    BufferedReader reader = new BufferedReader(new InputStreamReader((connection.getInputStream())));
    String splitreader = "";
    StringBuilder ss = new StringBuilder();
    while((splitreader = reader.readLine()) != null){
        ss.append(splitreader);
}
    JSONObject json = new JSONObject(ss.toString());
    JSONObject jsonrequest = json.getJSONObject("queries").getJSONArray("request").getJSONObject(0);
    return jsonrequest.getInt("totalResults");
    }


 public void doTrustToCertificates() throws Exception {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
                    return;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
                    return;
                }
            }
    };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
                System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '" + session.getPeerHost() + "'.");
            }
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

Exception

java.lang.NoSuchMethodError: sun.security.ssl.SSLSessionImpl.(Lsun/security/ssl/ProtocolVersion;Lsun/security/ssl/CipherSuite;Ljava/util/Collection;Lsun/security/ssl/SessionId;Ljava/lang/String;I)V

But codes worked on glassfish 4.1.1 and wildfly i can't understand why not working on glassfish 5.0

Check your Java version. This is due to changes in the JDK around JDK 8u161 which affect Grizzly.

To summarise:

  • If you use GlassFish 5.0, you need to be on a version of JDK 8 below JDK 8u161
  • If you use GlassFish 5.0.1 or higher, you need to be on a version of JDK 8 above 8u161

This bug is tracked in the GlassFish GitHub issue tracker:
https://github.com/eclipse-ee4j/glassfish/issues/22436

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM