Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java - path to trustStore - set property doesn't work?

I've setup a self-signed certificate to test an ssl java connection - however, it is refusing to locate the java trustStore. I've saved copies of it in /Java/jre6/lib/security in addition to the folder where the classes are compiled to (im using netbeans) and also to /java/jre6/bin none of the above appears to work, because when i run the following - trustStore = null.

public class ShowTrustStore {      public static void main(String[] args) {          System.setProperty("javax.net.ssl.keyStore", "keystore.jks");         System.setProperty("javax.net.ssl.trustStrore", "cacerts.jks");         System.setProperty("javax.net.ssl.keyStorePassword", "changeit");            String trustStore = System.getProperty("javax.net.ssl.trustStore");         if (trustStore == null) {             System.out.println("javax.net.ssl.trustStore is not defined");         } else {             System.out.println("javax.net.ssl.trustStore = " + trustStore);         }     } } 

how to set the path correctly?

**********UPDATE************ Using the getFile() method and some more debug data:

package ssltest;  public class Main {      public static void main(String[] args) {  //        System.setProperty("javax.net.ssl.keyStore", "/keystore.jks"); //        System.setProperty("javax.net.ssl.trustStrore", "/java.home/cacerts.jks"); //        System.setProperty("javax.net.ssl.keyStorePassword", "changeit"); //        System.setProperty("javax.net.ssl.trustStorePassword", "changeit");          try {             Main.class.getResource("trustStore.jks").getFile();         } catch (Exception e) {             e.printStackTrace();         }          String trustStore = System.getProperty("javax.net.ssl.trustStore");          if (trustStore == null) {             String storeLoc;             storeLoc = System.getProperty("java.class.path");             System.out.println("classpath: " + storeLoc);         }          trustStore = System.getProperty("javax.net.ssl.trustStore");         if (trustStore == null) {             System.out.println("javax.net.ssl.trustStore is not defined");         } else {             System.out.println("javax.net.ssl.trustStore = " + trustStore);         }     } } 

run: java.lang.NullPointerException classpath: C:\Users\Main\Documents\NetBeansProjects\sslTest\build\classes;C:\Users\Main\Documents\NetBeansProjects\sslTest\src at ssltest.Main.main(Main.java:15) javax.net.ssl.trustStore is not defined BUILD SUCCESSFUL (total time: 0 seconds)

like image 720
oneAday Avatar asked Jan 26 '10 09:01

oneAday


People also ask

Where is my truststore path?

The truststore comes bundled with the JDK/JRE and is located in $JAVA_HOME/lib/security/cacerts .

Where is the default Java truststore?

Java has bundled a truststore called cacerts, and it resides in the $JAVA_HOME/jre/lib/security directory.


1 Answers

You have a typo - it is trustStore.

Apart from setting the variables with System.setProperty(..), you can also use

-Djavax.net.ssl.keyStore=path/to/keystore.jks 
like image 125
Bozho Avatar answered Sep 20 '22 20:09

Bozho