Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Test of java SSL / keystore / cert setup. Check the commet #1 for howto.
/** Establish a SSL connection to a host and port, writes a byte and
* prints the response. See
public class SSLPoke {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: "+SSLPoke.class.getName()+" <host> <port>");
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(args[0], Integer.parseInt(args[1]));
InputStream in = sslsocket.getInputStream();
OutputStream out = sslsocket.getOutputStream();
// Write a test byte to get a reaction :)
while (in.available() > 0) {
System.out.println("Successfully connected");
} catch (Exception exception) {

4ndrej commented Jan 16, 2013

Test of java SSL / keystore / cert setup. Came from


  1. extract cert from server:
    openssl s_client -connect server:443
  2. negative test cert / keytool:
    java SSLPoke server 443
    you should get something like PKIX path building failed: unable to find valid certification path to requested target
  3. import cert into default keytool:
    keytool -import -alias -keystore $JAVA_HOME/jre/lib/security/cacerts
  4. positive test cert / keytool:
    java SSLPoke server 443
    you should get this:
    Successfully connected

jdros commented May 21, 2015

Thanks! Helped us to debug a cert issue.

Doesn't seems work as I thought it should work. it doesn't check if the server certificate is matching with the parameter.


java SSLPoke ip.address.of.sslserver 443
Successfully connected

So it doesn't really check the certificate?

bekce commented Nov 5, 2015

@jackchen858 +1 It does not

It did not work for me.

I always get Successfully connected msg

janeklb commented Jan 12, 2016

Make sure you run this with the right java in case you have multiple installations

Works brilliantly. Thanks

smeduru commented May 11, 2016

Thanks a lot. Followed your instructions. Fix worked perfectly.

dadez commented Nov 29, 2016

How to use it behind a proxy ?

joerg commented Jan 19, 2017

For those not living in the Java World here is how I compiled and used this:

  • Copy code to somewhere
  • Call Java compiler /usr/java/jdk1.6.0_45/bin/javac /tmp/ (use your version of Java here)
  • Call tool with ClassPath (-cp) that you copied the file to: /usr/java/jdk1.6.0_45/bin/java -cp /tmp SSLPoke 443

Very useful thanks.

A note however, instead of updating the java JRE/JDK installation's keystore, best practices dictates that you should define your own truststore (if you have company CA or application certificates for example):

# import certificate into your local TrustStore
keytool -import -trustcacerts -storepass changeit -file "./class 1 root ca.cer" -alias C1_ROOT_CA -keystore ./LocalTrustStore
# use it in JAVA:
java -jar SSLPoke.jar $HOST $PORT

jmara commented Feb 27, 2017

Will the default trustStore be overwritten by or is the new trustStore an addition to the default one? @Tzaphkiel

gbenmansour commented Apr 4, 2017

Thanks for sharing. When I try the negative test , I have the exception : java.lang.RuntimeException: Could not generate DH keypair
at SSLPoke.main(
Caused by: java.lang.RuntimeException: Could not generate DH keypair
... 2 more
Caused by: Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)
at com.sun.crypto.provider.DHKeyPairGenerator.initialize(DashoA13*..)

Any idea why I have this ? Another thing can you tell me how I can generate a certificate file from a server ?

@jmara will override the default truststore (cacerts). You can copy the default one and then add your cert and set it via so you don't lose the default CAs.

Works well! Thanks

P.S. If you don't use the default keystore, you'll need to pass it and the password for the keystore into your command as arguments.

Thanks guys, these steps helped me debug why a couple of Atlassian products couldn't talk to each other. I got it working for now, but in my "ideal" world since every release of an Atlassian product includes it's own JRE, I will automate the above steps into a script to inject the "peer" applications' (hosted on other servers) certificates into only the "vendored" JRE cacerts to allow them to trust each other and this way I'm not polluting the system but I can link all the applications to each other without a bunch of warnings and failures.

Thanks guys, just a +1 that is helping me debug an SSL issue on Weblogic between AdminServer and NodeManager.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment