How to properly package keystore in .jar file

I’m at the final step of developing (and especially deploying) a client server application where I have a secured connection using TLS. I got the keystore and truststore for the client and the server and currently I load them as SystemProperties before creating the SSLSocket / SSLServerSocket like this:

    Properties systemProperties = System.getProperties();
    systemProperties.put("", "./auth/labkey.jks");
    systemProperties.put("", "<somepassword>");
    systemProperties.put("", "./auth/labtrust.jks");
    systemProperties.put("", "<somepassword>");

This works fine but – as I already thought – doesn’t when packaging everything into a .jar file. Currently my project folder consists of the normal src folder in which I keep all the packages with the sourcecode files. The keystore and truststore are kept in a folder called “auth” which is on the same level as the src folder.

I know from previous projects that the relative path changes when packaging additional files into the .jar however the first thing is I don’t know how I should properly handle these file to have them packed into the .jar (currently they are only included when I create a package for them in the source folder or declare the auth folder as an additional source folder).

I tried both approaches and after testing a lot of different relative paths (which is the second problem: finding the right relative path) I couldn’t figure out how to properly address them.

Any advice or hint would be really appreciated.


After trying out several things in context with suggested ideas I can narrow the problem down to the path to the keystore and truststore file being my actual problem. I’m quite sure if I were able to access it relatively there would be an option to use, however for accessing the files I HAVE to provide a path in any kind, no matter which approach I’m going to use. The fact that I’m not able to get the proper relative path to the file is therefore the main problem which I need to get solved.

So accordingly these questions come to my mind:

  1. How do I correctly add these files to the .jar?
  2. Will I need to make the auth folder a source folder or do I need to put it in a package or is there any need or possibility to configure the files correctly by configuring the build path?
  3. What will then be the relative path to the files?

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
newest oldest most voted
Notify of

Quick edit: this has been answered before in previous threads – I’m sure my explanation is sub-par here, but this came up recently for me as well and this was my solution You may want to use getClass().getClassLoader.getResourceAsStream(“”) Properties properties = new Properties(); String propertiesFileName = ""; inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFileName); if (inputStream != null) { properties.load(inputStream); } else { throw new FileNotFoundException("property file '" + propertiesFileName + "' not found."); }