我有一个简单的应用程序,可以读取和写入属性文件。它是在 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/*.properties 在您的 jar 文件中。如果是这种情况,那么如果文件命名正确,您的代码应该可以工作。
关于java - 为什么我的 Java 应用程序找不到我的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14286000/