简体   繁体   中英

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.

Try

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

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

抱歉,在“conf /”之后删除星号(*)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM