问题
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.
回答1:
Try
java -cp "./*;lib/*;conf" com.myapp.Test
Asterix (*) is used to find all JARs not classes.
回答2:
Sorry, remove asteriks(*) after "conf/".
来源:https://stackoverflow.com/questions/34857705/add-properties-file-on-classpath