title: SSL Connection from Java client
body: <p>I'm creating a Java client program that will be sending sensitive information to a Tomcat server.  So I need to use SSL Connection so information will be encrypted.</p>
  
  <p>I need to use self-signed untrusted certificate but having problems making connection from java client.</p>
  
  <p>I have successfully setup Tomcat 5.5 to use SSL and tested it through Firefox, which displays warning of self-signed certificate.</p>
  
  <p>I followed the Tomcat 5.5 SSL setup and they mentioned to create a keystore:</p>
  
  <blockquote>
    <p><code>keytool -genkey -alias tomcat -keyalg RSA</code></p>
  </blockquote>
  
  <p>Then I did an export of the above:</p>
  
  <blockquote>
    <p><code>keytool -export -keystore .keystore -alias tomcat -file localhost.cer</code></p>
  </blockquote>
  
  <p>Then I did an import of the above certificate into client machine:</p>
  
  <blockquote>
    <p><code>keytool -import -alias tomcat -file localhost.cer -keystore "C:\Program Files"\Java\jdk1.6.0_17\jre\lib\security\cacerts"</code></p>
  </blockquote>
  
  <p>But when running client I get:</p>
  
  <blockquote>
    <p>Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target</p>
  </blockquote>
  
  <p>This is the client code:</p>
  
  <pre><code>URL url = new URL("https://localhost:8443");
  HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
  conn.setSSLSocketFactory(sslsocketfactory);
  InputStream inputstream = conn.getInputStream();
  </code></pre>
  
  <p>Now I just started playing with these certificates today and I'm new to keystores, so please be patient.</p>
  
  <p>Can someone please explain how to export and import the certificate created in Tomcat to client machine?</p>
  
  <p>Thank you.</p>