saeedj
saeedj

Reputation: 2419

Issue in accessing Spring boot application resource file in linux

Overview:

I use the following code to create the keystore from the certificate file which is in resources/certificates/crt.p12:

public static KeyStore getKeyStoreFromFile(String certificateFilePath,
        String certificatePassword) throws KeyStoreException {
        File p12File = new File(
            KeyStoreUtil.class.getClassLoader().getResource(certificateFilePath).getFile());
        KeyStore.Builder builder = KeyStore.Builder.newInstance("PKCS12", null, p12File,
            new KeyStore.PasswordProtection(certificatePassword.toCharArray()));
        return builder.getKeyStore();
    }

and it works in windows platform.

Issue:

However, when I run it on a linux platform the system cannot find the file and throws the following exception:

java.lang.IllegalArgumentException: File does not exist or it does not refer to a normal file: file:/executable/billpay-billinfo-services.jar!/certificates/crt.p12

I think it's the different platform issue and I would be grateful if any one can help me find solution for this problem.

Upvotes: 0

Views: 1218

Answers (2)

saeedj
saeedj

Reputation: 2419

First of all I understood that putting certificate file in classpath is a bad practice as it maybe needed to be changed in future. So I excluded it from the classpath and put it along side my jar file. This time when I run my test the certificate file was accessible.

Upvotes: 1

Issam El-atif
Issam El-atif

Reputation: 2486

Like this Classpath resource not found when running as jar but not a duplicate, not the same issue

resource.getFile() expects the resource itself to be available on the file system, i.e. it can't be nested inside a jar file. Use resource.getInputStream().

Source : Classpath resource not found when running as jar

Upvotes: 0

Related Questions