'specify files in resources folder in spring application.properties file

I have a Spring Boot application, the code need to access a file under resources folder. here is my application.properties file:

cert.file=classpath:/resources/cert.p12

however it always complained:

java.io.FileNotFoundException: classpath:/resources/cert.p12 (No such file or directory)

I double checked folder my_project/target/classes to make sure the file cert.p12 exits there.

and in the code I tried to access the file:

@Value("${cert.file}")
private String certFile;
....
@Bean
public Sender sender() {
    return new Sender(certFile);
}

what exactly is the this classpath? and why it cannot find the file? Thanks!



Solution 1:[1]

Classpath includes what you have inside you resources dir.

Try:

cert.file=classpath:cert.p12

I'm assuming that you have standard maven catalog structure.

Solution 2:[2]

This syntax doesn't work with a normal FileInputStream. Use the Spring Resourceloader instead.

@Autowired
private ResourceLoader resourceLoader;

@Value("${property.name}")
private String property;

File getPropertyFile(){
    return resourceLoader.getResource(property).getFile();
}

application.properties

property.name=classpath:filename.txt

Solution 3:[3]

You can just use XXX.class.getResourceAsStream("filename") to get a resource. such as:

ObjectInputStream ois = new ObjectInputStream(MyClass.class.getResourceAsStream(PUBLIC_KEY_FILE));
        Key key = (Key) ois.readObject();
        ois.close();

And, this is work in my code.The MyClass is the class witch use your crt file. My PUBLIC_KEY_FILE is "/rsa/PublicKey" and just store at the src/main/resources/rsa folder

resources location

Solution 4:[4]

as @BeeNoisy said, you should use getResourceAsSreame(...) instead of getResource(...).getFile().

i see exactly your problem and my code ran in my computer correctly but when i load app with embedded tomcat with java -jar command i see this error:

so i change code like this and error resolved:

private final String licencePass;
private final String licenceName;

public ProcessFormController(@Value("${ramona.licence.keystore.fullname}") String licenceName,
                             @Value("${ramona.licence.pass}") String licencePass) throws Exception {
    this.licenceName = licenceName;
    this.licencePass = licencePass;
    this.restTemplate = new RestTemplate(getHttpsRequestFactory());
}

private ClientHttpRequestFactory getHttpsRequestFactory() throws Exception {
    logger.info("licenceName:" + licenceName);
    final InputStream resourceAsStream =
            getClass().getClassLoader().getResourceAsStream(
                    licenceName
            );
    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
    keyStore.load(resourceAsStream, licencePass.toCharArray());
    ...
}

properties:

ramona.licence.keystore.fullname=key.p12

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raduy
Solution 2 Domo
Solution 3 BeeNoisy
Solution 4 amir azizkhani