title: Where to put the SSL keystore (.jks) and how to access it from .jar?
body: <p>I am working on a Java application that uses <strong>SSL</strong> to communicate with <strong>MongoDb</strong>. The application is designed to be packaged into a <strong>.jar</strong> file and then runs inside a Docker container. MongoDb requires you to setup JVM system properties in order to get SSL to work:</p>
  
  <pre><code>System.setProperty("javax.net.ssl.keyStore", pathToJKS);
  System.setProperty("javax.net.ssl.keyStorePassword", pw); 
  </code></pre>
  
  <p>My problem now is that I don't know where to put my .jks file and specify the correct path in order for my application to be able to access it at runtime?</p>
  
  <p>Currently my .jks file is located in the resources folder (src/main/resources/store.jks), which is fine if I execute the app via IDE but of course as soon as the app is executed from a .jar file, I am getting a <code>java.io.FileNotFoundException</code>. Below is the code I am currently using to get the path.</p>
  
  <pre><code>boolean isIDE = ...;
  String pathToJKS = isIDE ? "src/main/resources/store.jks" : getPathFromJar("store.jks");
  
  public String getPathFromJar(String filename) {
      ClassLoader classLoader = getClass().getClassLoader();
      final URL resourceUrl = classLoader.getResource(filename);
  
      if (resourceUrl != null) {
          return resourceUrl.toExternalForm();
      } else {
          return null;
      }
  }
  </code></pre>
  
  <p><strong>My question is where is the proper location to put the .jks file and how can I access it (via path) from a .jar file?</strong></p>