23

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!

2
  • 1
    have you managed to solve this? Commented Oct 18, 2017 at 16:21
  • 1
    is there a solution for this?
    – Vishal
    Commented Apr 25, 2018 at 13:40

6 Answers 6

20

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.

1
  • 4
    its threw the same error: FileNotFoundException: classpath:cert_key.p12.I use standard maven structure
    – user468587
    Commented Aug 31, 2015 at 17:17
10

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
1
  • when i run app with java -jar this code is not working. Commented Mar 31, 2020 at 5:15
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

0

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
0

Sharing my experience

Step 1 : Create your resource file lets say under /src/main/resources/data/test.data
Step 2 : Define the value in application.properties/yml

com.test.package.data=#{new org.springframework.core.io.ClassPathResource("/data/test.data").getFile().getAbsolutePath()}

Step 3 : Get the file in your code

@Value("${com.test.package.data}")
private String dataFile;

private void readResourceFile() {
   Path path = Paths.get(dataFile);
   List<String> allLines = Files.readAllLines(path);
}
-3

This path worked for me cert.file=./build/resources/main/cert.p12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.