简体   繁体   中英

Java : How to include a .properties file that is outside of jar into classpath?

We would like to keep the .properties file out of the jar so that we can change the properties used in a module and we do not have to re-install the module.

Before Java 8, we used to run a script and include the .properties file in the way below and it worked. But since when we updated to Java 8 , this way of including .properties file in classpath is not working, means java program fails not finding the .properties file.

My script to run the java project:

/usr/java/latest/bin/java -d64 -Xms1G -Xmx16G -XX:MaxPermSize=128m -XX:-UseGCOverheadLimit -cp "/online/sand/lib/client-api-1.0.0.jar:/online/sand/oap_pub/lib/*:/online/sand/oap/oap_dw/run/client_api/application.properties" team.online.client.api.MasterProcessor  | tee -a client_api.log

We are using Sping context to pick up the properties file this way:

<util:properties id="app_props"
                 location="classpath*:/application.properties" />

Then a property in that appilcation.properties files is being used ( in many different files) this way:

@Value( "#{app_props[\"SERVICE_PATH_GET_METADATA\"]?:''}" )
private String metadataServicePath;

Looking for a way to keep the.properties file out of the jar and in classpath so that Spring context finds that file.

Is their any other way than using ? We need to keep the properties file excluded from jar

class.getClassLoader( ).getResourceAsStream( "application.properties" );

Thanks in advance.

Use this if you are using xml based config

<context:property-placeholder location="classpath:application.properties"/>

Then it will load the properties from anywhere as long as it is in class path not in the jar. You have to include it in class path as $CLASSPATH:application.properties

For annotation based, @PropertySource("classpath:application.properties") There is a sample here Java: read properties file using spring annotations

I found a workaround making changes in the script to run the program by using "::" instead of ":" .

Got the idea :

==> java and javac are handling the classpath settings in a different way, javac is searching for classes in / if the classpath contains double Classpath delimiters ("::" under Unix, ";;" under Windows).

From this link which seemed convincing to me and that works. Please take a look here: https://bugs.openjdk.java.net/browse/JDK-4809833

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