Reputation: 5031
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!
Upvotes: 23
Views: 54455
Reputation: 5257
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);
}
Upvotes: 0
Reputation: 1804
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
Upvotes: 0
Reputation: 609
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
Upvotes: 10
Reputation: 1374
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
Upvotes: 3
Reputation: 1464
This path worked for me cert.file=./build/resources/main/cert.p12
Upvotes: -3
Reputation: 540
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.
Upvotes: 20