0

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.

saeedj
  • 2,179
  • 9
  • 25
  • 38
  • Does the user that runs the java program on linux has the privilege to read and use this file? – Nadir Oct 10 '16 at 07:26

2 Answers2

1

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.

saeedj
  • 2,179
  • 9
  • 25
  • 38
  • I was also getting this error with a certificate file that was inside the jar file and my solution was to place it in an accessible url to be downloaded using Apache Commons (FileUtils.copyURLToFile), thanks to this the error the error has not occurred again – ErisoHV Jun 08 '21 at 14:42
0

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

Community
  • 1
  • 1
Issam El-atif
  • 2,366
  • 2
  • 17
  • 22