How to solve javax.net.ssl.SSLHandshakeException Error?
The javax.net.ssl.SSLHandshakeException error is usually caused by a problem with the SSL/TLS certificate of the server you are trying to connect to.
The javax.net.ssl.SSLHandshakeException error is usually caused by a problem with the SSL/TLS certificate of the server you are trying to connect to. This can happen if the certificate is self-signed or not trusted by the client, or if the certificate has expired or has been revoked.
To solve this error, you can try the following:
- Check the server certificate: Make sure that the server certificate is valid and not expired. You can verify it using
openssl:
openssl s_client -connect example.com:443 -showcertsYou can also inspect Java's default truststore with keytool:
keytool -list -v -keystore $JAVA_HOME/lib/security/cacerts- Trust the server certificate: If the server certificate is self-signed or not trusted by your client, import it into the JVM truststore using
keytool:
keytool -importcert -alias myserver -file server.crt -keystore $JAVA_HOME/lib/security/cacerts -storepass changeitAlternatively, specify a custom truststore at runtime using JVM system properties:
java -Djavax.net.ssl.trustStore=/path/to/truststore.jks -Djavax.net.ssl.trustStorePassword=changeit MyApp- Use a different SSL/TLS protocol: If the server supports multiple SSL/TLS protocols, you can force a specific version via JVM properties:
java -Dhttps.protocols=TLSv1.2 MyAppFor programmatic control, configure an SSLContext with the desired protocol before creating your connection.
- Check for network and hostname issues: Ensure no firewall or proxy is intercepting the connection. In Java, mismatched hostnames often trigger this exception. You can disable strict hostname verification for testing (not recommended for production):
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);I hope this helps! Let me know if you have any questions.