为什么我的 Java 应用程序找不到我的属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14286000/
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 15:45:53  来源:igfitidea点击:

Why can't my Java Application find my properties file

javapropertiesjar

提问by nathj07

I have a simple application that reads from and writes to a properties file. It was developed in NetBeans and when run from within Netbeans works just fine. However, now I have built and deployed it the properties file cannot be found.

我有一个简单的应用程序,可以读取和写入属性文件。它是在 NetBeans 中开发的,在 Netbeans 中运行时运行良好。但是,现在我已经构建并部署了它,但找不到属性文件。

Project Structure

项目结构

com/company/projectname/controller/controllerclass.java <- this is where the code using the properties file exists

conf/runController.properties <- the properties file, this exists here

In the code I have the following to access the properties file:

在代码中,我有以下内容来访问属性文件:

private final String PROPERTIESFILELOCATION = "conf/runController.properties";

public void runController(){
this.runController = new Properties();
this.propertiesLocation = this.getClass().getClassLoader().getResource(this.PROPERTIESFILELOCATION);
FileInputStream propsInput = new FileInputStream(this.propertiesLocation.getFile());
this.runController.load(propsInput);
}

(summarized for brevity)

(为简洁起见总结)

When I call the jar file from the command line I issue:

当我从命令行调用 jar 文件时,我发出:

java -classpath /usr/java/projectdirectory/project.jar com.company.projectname.controller.controllerclass arg1

So, I have managed to achieve this before on other projects in just this way but for some reason this is not working.

因此,我之前已经以这种方式在其他项目中设法实现了这一点,但由于某种原因,这不起作用。

I have checked the structure inside the jar file and all is as expected in there.

我已经检查了 jar 文件中的结构,一切都在那里。

Can anyone point out my mistake and help me get this up and running please?

任何人都可以指出我的错误并帮助我启动并运行它吗?

EDIT- changed the names to match across. They were always consistent in my code

编辑 - 更改名称以匹配。它们在我的代码中始终保持一致

采纳答案by nathj07

Thanks to everyone for helping out, especially dashrb, plus 1 for your code.

感谢大家的帮助,尤其是 dashrb,为您的代码加 1。

I managed to get it working with your help. The final solution to this problem was a slight change of tack.

在你的帮助下,我设法让它工作。这个问题的最终解决方案是稍微改变一下策略。

As I had the need to read and write to the file (probably not clear in my OP) I switched to using Apache.commons.configuration.

由于我需要读取和写入文件(可能在我的 OP 中不清楚),我转而使用 Apache.commons.configuration。

That said the pointers in this thread did ensure my other properties files were working without hitch.

也就是说,该线程中的指针确实确保了我的其他属性文件可以顺利运行。

Thanks again

再次感谢

回答by Kal

Your question talks about

你的问题是关于

conf/controller.properties

conf/controller.properties

and your code talks about conf/runController.properties

你的代码谈到 conf/runController.properties

EDIT: I'm assuming that the conf/*.properties is inside your jar file. If thats the case, then your code should work if the files are named correctly.

编辑:我假设 conf/*.properties 在您的 jar 文件中。如果是这样,那么如果文件命名正确,您的代码应该可以工作。

回答by dashrb

Perhaps the FileInputStream isn't able to honor the properties "file" which is inside your jar file. Change your:

也许 FileInputStream 无法遵守 jar 文件中的属性“文件”。改变你的:

this.runController = new Properties();
this.propertiesLocation = this.getClass().getClassLoader().getResource(this.PROPERTIESFILELOCATION);
FileInputStream propsInput = new FileInputStream(this.propertiesLocation.getFile());
this.runController.load(propsInput);

to:

到:

this.runController = new Properties();
this.runController.load(this.getClass().getClassLoader().getResourceAsStream(this.PROPERTIESFILELOCATION));

EDIT: I created a test class, and it shows that when running from the filesystem OR from a JAR file, that "props/main.properties" works but "/props/main.properties" does not:

编辑:我创建了一个测试类,它显示当从文件系统或 JAR 文件运行时,“props/main.properties”有效但“/props/main.properties”无效:

[rtb@rtblinux props]$ cat org/dashrb/test/main.java 
package org.dashrb.test;
import java.util.Properties;
import java.io.IOException;

public class main
{
    public static void main(String args[])
    {
        main myMain = new main();
        myMain.testProps("props/main.properties");
        myMain.testProps("/props/main.properties");
    }

    public main()
    {
    }

    public void testProps(String p)
    {
        try
        {
            System.out.println("===============================");
            Properties props = new Properties();
            System.out.println("Trying to load properties as " + p);
            props.load(getClass().getClassLoader().getResourceAsStream(p));
            System.out.println("Loaded properties as " + p);
            System.out.println("Property x is: " + props.getProperty("x"));
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
        System.out.println("===============================");
    }
}

[rtb@rtblinux props]$ cat props/main.properties 
x = This is the property value of x

[rtb@rtblinux props]$ java -cp . org.dashrb.test.main
===============================
Trying to load properties as props/main.properties
Loaded properties as props/main.properties
Property x is: This is the property value of x
===============================
===============================
Trying to load properties as /props/main.properties
Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at org.dashrb.test.main.testProps(main.java:25)
    at org.dashrb.test.main.main(main.java:11)




[rtb@rtblinux props]$ jar cvf main.jar org props
added manifest
adding: org/(in = 0) (out= 0)(stored 0%)
adding: org/dashrb/(in = 0) (out= 0)(stored 0%)
adding: org/dashrb/test/(in = 0) (out= 0)(stored 0%)
adding: org/dashrb/test/main.class(in = 1218) (out= 679)(deflated 44%)
adding: org/dashrb/test/main.java(in = 594) (out= 287)(deflated 51%)
adding: props/(in = 0) (out= 0)(stored 0%)
adding: props/main.properties(in = 36) (out= 36)(deflated 0%)

[rtb@rtblinux props]$ jar tvf main.jar 
     0 Fri Jan 11 17:29:40 EST 2013 META-INF/
    68 Fri Jan 11 17:29:40 EST 2013 META-INF/MANIFEST.MF
     0 Fri Jan 11 17:26:00 EST 2013 org/
     0 Fri Jan 11 17:26:00 EST 2013 org/dashrb/
     0 Fri Jan 11 17:29:24 EST 2013 org/dashrb/test/
  1218 Fri Jan 11 17:28:52 EST 2013 org/dashrb/test/main.class
   594 Fri Jan 11 17:29:24 EST 2013 org/dashrb/test/main.java
     0 Fri Jan 11 17:26:40 EST 2013 props/
    36 Fri Jan 11 17:26:40 EST 2013 props/main.properties

[rtb@rtblinux props]$ cd /
[rtb@rtblinux /]$ java -cp ~/misc/src/java/props/main.jar org.dashrb.test.main
===============================
Trying to load properties as props/main.properties
Loaded properties as props/main.properties
Property x is: This is the property value of x
===============================
===============================
Trying to load properties as /props/main.properties
Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at org.dashrb.test.main.testProps(main.java:25)
    at org.dashrb.test.main.main(main.java:11)

There must be something different in your situation which is preventing your success.

你的情况一定有什么不同的东西阻碍了你的成功。

回答by Nathan Hughes

If you use getResourceto open the properties file, that assumes that the file is in the classpath, so you need to put the properties file in the classpath. The alternatives are to add the conf directory to the classpath, or move the properties file so that it is in the existing classpath.

如果使用getResource打开属性文件,则假定该文件在类路径中,因此您需要将属性文件放在类路径中。替代方法是将 conf 目录添加到类路径,或移动属性文件,使其位于现有的类路径中。

One thing that might help is to refer to the file location with a starting slash, so there is no doubt that you want to start searching for the file from the root of the classpath. Otherwise the search path is relative to the class your code is making the call from (don't know that this is right; that's how it works with Class.getResource, Classloader.getResource may be different though).

可能有帮助的一件事是使用起始斜杠引用文件位置,因此毫无疑问您想从类路径的根目录开始搜索文件。否则,搜索路径是相对于您的代码正在调用的类(不知道这是正确的;这就是它与 Class.getResource 一起工作的方式,但 Classloader.getResource 可能会有所不同)。