I am building a Spring standalone application which is based on Spring Boot. I want this application to read its properties from a property file which is outside of jar file in a separate directory. The structure of the built application looks like this
testApplication
├── test-1.0-SNAPSHOT.jar
├── lib
│ └── <dependencies are here>
└── conf
└── application.properties
I want to load the application.properties file on classpath, so I am able to read 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 following exeption:
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 the file from application by this Class annotation:
@PropertySource("classpath:application.properties")
Java documentation does not mention, that a non-jar files can be loaded on classpath, but if this application.properties is in the jar file test-1.0-SNAPSHOT.jar, then it can be accessed by this way.
Is it possible to put external non-jar file on classpath? I know I don't necessarily have the file on classpath, I can access it by different ways but still I am curious if it is possible.
Go to Project Structure | Modules | Your Module | Dependencies, click Add, Single-Entry Module Library, specify the path to the "resources" folder. Yet another solution would be to put the log4j. properties file directly under the Source root of your project (in the default package directory).
Try
java -cp "./*;lib/*;conf" com.myapp.Test
Asterix (*) is used to find all JARs not classes.
Sorry, remove asteriks(*) after "conf/".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With