1

I use ssl to secure RMI and JMS communications between clients and server. I add keyStore and trustStore files to resource folder of my project. When the project run through Netbeans, these file are loaded without any problem, using below codes:

System.setProperty("javax.net.ssl.keyStore", "src/main/resources/certificates/keyStore.ks");
System.setProperty("javax.net.ssl.keyStorePassword", "pass");
System.setProperty("javax.net.ssl.trustStore", "src/main/resources/certificates/trustStore.ts");
System.setProperty("javax.net.ssl.trustStorePassword", "pass"); 

But, when I run this project through its jar file, these files cannot be loaded.

Is there any alternate way to config SSL instead of using System.setProperty() and specifying the paths to keyStore and trustStore files?

Also, it works in both modes (run through Netbeans and Jar file).

Phani Kumar M
  • 4,564
  • 1
  • 14
  • 26
M. Jamshidi
  • 35
  • 1
  • 7

1 Answers1

2

The src directory isn't there at runtime. Check the contents of the JAR file and see for yourself. You will have to put them somewhere else, and specifically not in the JAR file at all, as these properties refer to files, not to resources in JAR files.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Because of some security issues, I don't want to store these keyStore and trustStore files outside of my jar file. – M. Jamshidi Oct 03 '17 at 13:13
  • Then you will need to accesses them *as resources*, via `Class.getResource()` and friends, and write your own `SSLContext`-initializing code, that loads keystores and truststores and initializes `KeyManagers` and `TrustManagers`, as shown in the JSSE Reference Guide. You can't use these syste properties for resources. – user207421 Oct 04 '17 at 00:41
  • Thanks dear. I found a solution same as you said: https://stackoverflow.com/questions/344748/how-to-use-a-file-in-a-jar-as-javax-net-ssl-keystore – M. Jamshidi Oct 08 '17 at 13:28