繁体   English   中英

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

[英]Why can't my Java Application find my properties file

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

项目结构

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

在代码中我有以下访问属性文件:

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);
}

(为简洁起见)

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

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

所以,我已经成功地在其他项目上实现了这一点,但由于某种原因,这是行不通的。

我已经检查了jar文件中的结构,并且所有内容都符合预期。

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

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

你的问题谈到了

CONF / controller.properties

和你的代码谈论conf/runController.properties

编辑:我假设conf / *。属性在你的jar文件中。 如果是这种情况,那么如果文件名称正确,那么您的代码应该可以正常工作。

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

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

至:

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

编辑:我创建了一个测试类,它显示当从文件系统或从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)

在你的情况下必须有不同的东西阻止你的成功。

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

可能有用的一件事是使用起始斜杠引用文件位置,因此毫无疑问您要从类路径的根开始搜索文件。 否则,搜索路径是相对于您的代码进行调用的类(不知道这是正确的;这是它如何与Class.getResource一起使用,但Classloader.getResource可能不同)。

感谢大家帮忙,特别是dashrb,还有1代码。

我设法让它在你的帮助下工作。 这个问题的最终解决方案是大幅改变。

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

这就是说这个线程中的指针确实确保我的其他属性文件无故障地工作。

再次感谢

暂无
暂无

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

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