Search code examples
javamavenresourceskeystoretruststore

How to load a keystore, which is inside the resource folder (maven)?


I have a custom SSL factory, where I load my own truststore.

Now when I put the truststore.jks file into the project root folder, it works with the following line:

ks.load(new FileInputStream("/truststore.jks", passphrase);

But I want my truststore inside my resource folder, which was built with maven where the path is src/main/resources.

Then I do and it doesn't work with the following line:

ks.load(this.getClass().getResourcesAsStream("/truststore.jks"), passphrase);

Though the input stream exists. I checked it. It only fails when I do ks.load(...).

The exception that I get is:

 java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

Why is that?

Regards, Dave


Solution

  • Strange, it works now...

    I had

    Properties systemProps = System.getProperties();
    systemProps.put( "javax.net.ssl.trustStore", "/truststore.jks");
    systemProps.put( "javax.net.ssl.trustStorePassword", "changeit");
    System.setProperties(systemProps);
    

    and change the second line to

    systemProps.put( "javax.net.ssl.trustStore", "src/main/resources/truststore.jks");
    

    Anyone knows why? Is this solution ok?