java 属性文件的java类路径问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14941423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:57:46  来源:igfitidea点击:

java class path issue with properties file

javabatch-fileclasspath

提问by Radan

I have a batch file to set class path before calling the java main method, see code below

在调用 java main 方法之前,我有一个批处理文件来设置类路径,请参阅下面的代码

SET CLASSPATH=%CLASSPATH%;libs/xyz1.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz2.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz3.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz4.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz5.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz6.jar
SET CLASSPATH=%CLASSPATH%;/resource

java -classpath %CLASSPATH% com.xyz.main

if I keep my properties file inside the resource folder it works fine, but if I leave it in root folder it doesn't work.

如果我将我的属性文件保存在资源文件夹中,它可以正常工作,但如果我将其保留在根文件夹中,则它不起作用。

SET CLASSPATH=%CLASSPATH%;libs/xyz1.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz2.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz3.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz4.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz5.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz6.jar

java -classpath %CLASSPATH% com.xyz.main

the above class path setting don't work am getting null pointer exception

上面的类路径设置不起作用正在获取空指针异常

SET CLASSPATH=%CLASSPATH%;libs/xyz1.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz2.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz3.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz4.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz5.jar
SET CLASSPATH=%CLASSPATH%;libs/xyz6.jar
SET CLASSPATH=%CLASSPATH%;/

java -classpath %CLASSPATH% com.xyz.main

the above class path setting don't work am getting the same null pointer exception.

上面的类路径设置不起作用我得到了相同的空指针异常。

Am using java util properties as below.

我正在使用 java util 属性,如下所示。

  InputStream inputStream = this.getClass().getResourceAsStream("/abc.properties");
  getProperties().load(inputStream);

it will be really nice if someone can help me find, why this happens. thank you in advance.

如果有人能帮我找到为什么会这样,那就太好了。先感谢您。

采纳答案by user1874565

Include complete class path

包括完整的类路径

java -classpath .;%CLASSPATH% com.xyz.main

java -classpath .;%CLASSPATH% com.xyz.main

you can also set multiple specification Refer the below link, http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/classpath.html

您还可以设置多个规范请参阅以下链接, http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/classpath.html

回答by AlexR

Call getResourceAsStream()causes java to look for a resource available for current class loader, i.e. one that can be found in your class path. If you want to read file from file system use newnew FileInputStream("abc.properties")` instead. If you want to continue reading file from resource you must include it into your classpath either into one of your jar files or as a separate entry.

调用getResourceAsStream()会导致 java 查找当前类加载器可用的资源,即可以在类路径中找到的资源。如果要从文件系统读取文件,请改用newnew FileInputStream("abc.properties")`。如果您想继续从资源中读取文件,您必须将它包含在您的类路径中,或者包含在您的 jar 文件之一中,或者作为单独的条目。

You can even implement logic that reads file from class path and then overrides the values with file found in file system.

您甚至可以实现从类路径读取文件的逻辑,然后使用文件系统中找到的文件覆盖值。

BTW take a look on Hymanarata configurationpackage that has this feature built-in.

顺便说一句,看看内置此功能的Hymanarata 配置包。

回答by asifsid88

This is because the classpath are checked from the current directory .would do the best
Even if you do not include your properties file in the resource folder and run the java program as mention below it will work perfectly

这是因为从当前目录检查类路径.会做得最好
即使你没有在资源文件夹中包含你的属性文件并运行下面提到的 java 程序它也能完美运行

java -cp .;%CLASSPATH% com.xyz.main

NOTE: cp is the shorthand for classpath

注意:cp 是类路径的简写

回答by CodeChimp

You need to include "./" in your CLASSPATH. You are effectively telling the JVM to exclude it.

您需要在 CLASSPATH 中包含“./”。您实际上是在告诉 JVM 将其排除在外。