title: Validating a certificate in java throws an exception - unable to find valid certificate path to requested target
body: <p>I have a web app that requires a client to send it's certificate and the server has to validate the certificate(i.e see if the issuer is a valid issuer and present in the server's truststore). Here is the code : <br></p>
  
  <pre><code>FileInputStream fin=new FileInputStream("C:/trustedca");
      KeyStore anchors = KeyStore.getInstance("JKS","SUN");
      anchors.load(fin, "server".toCharArray());
      X509CertSelector target = new X509CertSelector();
      FileInputStream fin1=new FileInputStream("C:/client.crt");
      CertificateFactory cf=CertificateFactory.getInstance("X.509");
      X509Certificate cert=null;
      while (fin1.available() &gt; 0) 
      {
       System.out.println("in while---------");
       cert =(X509Certificate) cf.generateCertificate(fin1);
      }
      target.setCertificate(cert);
      PKIXBuilderParameters params = new PKIXBuilderParameters(anchors, target);
  
      CertPathBuilder builder = (CertPathBuilder) CertPathBuilder.getInstance("PKIX").build(params);
      PKIXCertPathBuilderResult r = (PKIXCertPathBuilderResult) builder.build((CertPathParameters)params);&lt;br&gt;
  </code></pre>
  
  <p>But  I get an exception : <br></p>
  
  <pre><code>sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid
   certification path to requested target&lt;br&gt;
  </code></pre>
  
  <p><strong>NOTE :</strong> <br>
  Here the certificate sent by the client is client.crt and the cerificate used to sign the client.crt certificate is the ca.crt which is present in the keystore "trustedca". Then why is it giving this exception?</p>