title: What causes SunCertPathBuilderException: unable to find valid certification path to requested target
body: <p>I am trying to run a very simple server/client ssl code with a self generated keystore file. I got the following exception. 
  I am sure the code has no problem because I can run it several weeks ago. But now I just get this exception:
  Could anyone tell me what's wrong?</p>
  
  <pre><code>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
      at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1649)
      at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241)
      at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235)
      at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1206)
      at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136)
      at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593)
      at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
      at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:632)
      at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:59)
      at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
      at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
      at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:276)
      at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:122)
      at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:212)
      at java.io.BufferedWriter.flush(BufferedWriter.java:236)
      at EchoClient.main(EchoClient.java:32)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
  Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:323)
      at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:217)
      at sun.security.validator.Validator.validate(Validator.java:218)
      at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126)
      at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209)
      at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)
      at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1185)
      ... 19 more
  Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174)
      at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)
      at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:318)
      ... 25 more
  </code></pre>
  
  <p>Following is my Code:</p>
  
  <pre><code>public
  class EchoServer {
      public static void main(String[] arstring) {
          System.setProperty("javax.net.ssl.keyStore", "c:\\projects\\sandbox\\SSLServer\\mySrvKeyStore");
          System.setProperty("javax.net.ssl.keyStorePassword", "delphi");
          try {
              SSLServerSocketFactory sslserversocketfactory =
                      (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
              SSLServerSocket sslserversocket =
                      (SSLServerSocket) sslserversocketfactory.createServerSocket(9999);
              SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();
  
              InputStream inputstream = sslsocket.getInputStream();
              InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
              BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  
              String string = null;
              while ((string = bufferedreader.readLine()) != null) {
                  System.out.println(string);
                  System.out.flush();
              }
          } catch (Exception exception) {
              exception.printStackTrace();
          }
      }
  }
  
  
  public
  class EchoClient {
      public static void main(String[] arstring) {
          System.setProperty("javax.net.ssl.keyStore", "c:\\projects\\sandbox\\SSLClient\\mySrvKeyStore");
          System.setProperty("javax.net.ssl.keyStorePassword", "delphi");
          try {
              SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
              SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999);
  
              InputStream inputstream = System.in;
              InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
              BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  
              OutputStream outputstream = sslsocket.getOutputStream();
              OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
              BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
  
              String string = null;
              while ((string = bufferedreader.readLine()) != null) {
                  bufferedwriter.write(string + '\n');
                  bufferedwriter.flush();
              }
          } catch (Exception exception) {
              exception.printStackTrace();
          }
  
      }
  }
  </code></pre>