简体   繁体   中英

Java not honoring the classpath specification with -cp option when running with a jar

I am running a program like the following:

java -cp /deploy/conf -jar test.jar

test.jar has a class that tries to load properties from a file located in /deploy/conf like the following:

Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties")

The thing is I print out the classpath from both the thread's class loader and the system class loader and neither contain the value /deploy/conf that I specified with the -cp option.

How can I make sure that java program passes along the values that I specify with -cp option to the thread's class loader?

From the Tools documentation for java with the -jar option:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

Usually this means you need to use a Manifest Class-Path entry. In this case you can't refer to an absolute path with a Class-Path entry, so you must load the file as a file, not as a resource.

您可以尝试直接使用FileInputStream而不是资源加载过程,例如,使用以下方法:

FileInputStream in = new FileInputStream ("/deploy/conf/config.properties");

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