简体   繁体   中英

How to add properties file to java classpath using command-line?

My current structure looks like

$ls
project.jar
propertiesFileA
propertiesFileB

The way I run my project, I do

java -jar project.jar MyClass 

It's a Spring based project where context.xml resolves some of the properties from propertiesFileA and propertiesFileB

So I get error because Spring can not find these files. That means I need to add them to classpath .

I tried following with no success at all

java -classpath .:/project.jar MyClass

java -cp "project.jar:." MyClass

java -classpath .;project.jar MyClass

Question

How can I provide propertiesFileA and propertiesFileB on command-line to MyClass ?

Regarding your question title, "How to add properties file to java classpath using command-line?", to be specific, you need to add to the class path the directory that contains your properties files.

For example, if your properties files are located at

  • /path/to/your/properties/files/file1.properties
  • /path/to/your/properties/files/file2.properties

Then what you need is to add their directory (/path/to/your/properties/files/, or /path/to/your/properties/files) to the class path, such as:

java -cp "/path/to/your/properties/files/:/path/to/your/project.jar:/path/to/3rdparty/jars/*" com.shn.ec.main.MyClass

java -cp "/path/to/your/properties/files:/path/to/your/project.jar:/path/to/3rdparty/jars/*" com.shn.ec.main.MyClass

The following helped me

java -cp ".:project.jar:3rdparty/*" com.shn.ec.main.MyClass

I was missing FQCN when calling out class

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