Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add properties file on classpath

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.

like image 702
Michal Krasny Avatar asked Jan 18 '16 14:01

Michal Krasny


People also ask

How add log4j properties file in classpath in Intellij?

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).


2 Answers

Try

java -cp "./*;lib/*;conf" com.myapp.Test

Asterix (*) is used to find all JARs not classes.

like image 129
clovish Avatar answered Oct 13 '22 00:10

clovish


Sorry, remove asteriks(*) after "conf/".

like image 40
eg04lt3r Avatar answered Oct 12 '22 23:10

eg04lt3r