title: How to add multiple truststore paths to “java.net.ssl.trustStore”?
body: <p>I want my Java Code to search for CA certificate of the server in one keystore... if it is unable to find the specific certificate (which I think will be known only when I try to connect via LDAP to Directory Server), it should look for the certificate in another keystore, whose path I know.</p>
  <p>I tried this:</p>
  <pre><code>System.setProperty(&quot;javax.net.ssl.trustStore&quot;, System.getProperty(&quot;java.home&quot;) + &quot;/lib/security/cacerts&quot; + System.getProperty(&quot;path.separator&quot;) + path/to/second/keystore);
  </code></pre>
  <p>But it didn't seem to work.</p>
  <p>Adding only one path (either of them) works, i.e. it runs like charm if certificate is found and fails if not.</p>
  <p>So my question is:</p>
  <ol>
  <li><p>Is there a method to add multpile keystore paths to javax.net.ssl.trustStore?</p>
  </li>
  <li><p>If it is not possible how should I write my code (I am asking for the algorithm) so that it just not throw Exception after first search itself and fail?</p>
  </li>
  </ol>
  <p>P.S. : I am not much familiar with Java.</p>
  <p>Below is the relevant section of my code:</p>
  <pre><code>if(useSSL)
  {
    try 
    {   
      SSLContext se = SSLContext.getInstance(&quot;TLS&quot;);
      Security.addProvider(se.getProvider());
    }   
    catch(NoSuchAlgorithmException e) { }
  
    System.setProperty(&quot;javax.net.ssl.trustStore&quot;, System.getProperty(&quot;java.home&quot;) + &quot;/lib/security/cacerts&quot;);
  
    com.org.ldap.LDAPSocketFactory ssf = new LDAPJSSESecureSocketFactory();
    LDAPConnection.setSocketFactory(ssf);
  }
  
  try 
  {   
    lc = new LDAPConnection();
    lc.connect( ldapServer, ldapPort);
    lc.bind( ldapVersion,  ldapUser, (userInfo[1]).getBytes() );
  }
  catch (LDAPException le)
  {
    le.printStackTrace();
  }
  </code></pre>