简体   繁体   中英

No such file or directory - When running a service on JAR file

I'm building a Spring Boot Application. I need to call outside files for using its service from my project.

Here is the root directory looks like,

在此处输入图片说明

I need to read files in the red box in my service and here is my example code,

@Value("${2c2p.payment-action-api.encrypt-key-file.path:cert/demo/demo2.crt}")
private String encryptKeyFilePath;
@Value("${2c2p.payment-action-api.decrypt-key-file.path:cert/demo/demo2.pfx}")
private String decryptKeyFilePath;

Where I call the encryptKeyFilePath or decryptKeyFilePath in my function on development (running via mvn spring-boot:run ) everything works perfectly.

But when I build the JAR file, those files can't be seen.

Do I need to add some config on application.properties or put the cert folder to somewhere else ?

Code when I call the file,

private String encrypt(String message) throws CertificateException, IOException {
    String base64Str = pk.encryptMessage(message, pk.getPublicCert(encryptKeyFilePath));
    return base64Str;
}

Please help. Thanks a lot.

You could use ClassPathResource , check the documentation :

https://www.baeldung.com/spring-classpath-file-access

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM