简体   繁体   English

Java与jar一起运行时不遵循-cp选项的类路径规范

[英]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: test.jar有一个试图从位于/ deploy / conf中的文件中加载属性的类,如下所示:

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. 事情是我从线程的类加载器和系统类加载器中打印出类路径,并且都不包含用-cp选项指定的值/deploy/conf

How can I make sure that java program passes along the values that I specify with -cp option to the thread's class loader? 如何确保Java程序将使用-cp选项指定的值传递给线程的类加载器?

From the Tools documentation for java with the -jar option: 从带有-jar选项的java 工具文档中:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. 使用此选项时,JAR文件是所有用户类的源,而其他用户类路径设置将被忽略。

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. 在这种情况下,您不能使用Class-Path条目引用绝对路径,因此必须将文件作为文件而不是作为资源加载。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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