Java – add a property file on the classpath
I am building a spring independent application based on spring boot I want this application to read its properties from properties files other than jar files in a separate directory The structure of the built application is as follows
testApplication ├── test-1.0-SNAPSHOT.jar ├── lib │ └── <dependencies are here> └── conf └── application.properties
I want to load the application on the classpath Properties file, so I can read it in my application Is it possible? Because when I run my jar file like this (on Windows):
java -cp "./*;lib/*;conf/*" com.myapp.Test
I get the following exceptions:
Exception in thread "main" org.springframework.beans.factory.BeanDeFinitionStoreException: Failed to parse configuration class [com.myapp.Test]; nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180) ... Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
I try to read files from the application through such comments:
@PropertySource("classpath:application.properties")
The Java documentation does not mention that you can load non jar files on the classpath, but if this application Properties in the jar file test-1.0-snapshot Jar, you can access it in this way
Can external non jar files be placed on the classpath? I know I don't necessarily have a file on the classpath. I can access it in different ways, but I'm still curious, if possible
Solution
attempt
java -cp "./*;lib/*;conf" com.myapp.Test
Asterix (*) is used to find all jars instead of classes