【发布时间】:2012-03-18 04:07:20
【问题描述】:
以下场景
./
config/
application.properties
lib/
properties-loader-0.0.1-SNAPSHOT.jar
acme.sh
properties-loader-0.0.1-SNAPSHOT.jar 是一个带有清单文件的可执行 jar。
jar 中有一个包com.acme.lab,只包含类
具有完全限定名称com.acme.lab.PropertiesLoader。
脚本acme.sh执行如下命令:
java -cp etc/application.properties:./lib/properties-loader-0.0.1-SNAPSHOT.jar com.acme.lab.PropertiesLoader
我正在尝试从PropertiesLoader 类访问属性文件。我读了Smartly load your properties的文章,但仍然无法访问属性文件
System.out.println(this.getClass().getResourceAsStream("../etc/application.properties"));
System.out.println(this.getClass().getResourceAsStream("etc/application.properties"));
System.out.println(this.getClass().getResourceAsStream("/etc/application.properties"));
System.out.println(this.getClass().getResourceAsStream("application.properties"));
System.out.println(this.getClass().getClassLoader().getResourceAsStream("../etc/application.properties"));
System.out.println(this.getClass().getClassLoader().getResourceAsStream("etc/application.properties"));
System.out.println(this.getClass().getClassLoader().getResourceAsStream("/etc/application.properties"));
System.out.println(this.getClass().getClassLoader().getResourceAsStream("application.properties"));
try {
System.out.println(ResourceBundle.getBundle("etc.application"));
System.out.println(ResourceBundle.getBundle("application"));
} catch(MissingResourceException e) {
// do nothing
}
所有这些调用都无法加载文件。
我只知道错误与类路径有关,但似乎找不到。我在 github 创建了一个示例 maven 项目,它重现了问题。
【问题讨论】:
-
@drhirsch 我认为标签
maven-assembly-plugin在这种情况下可能比assemblies更合适。 -
当然,将其更改为您认为合适的 - 但
assembly和以前一样,是错误的。事实上,正是这个标签把我带到了这里——它是关于机器代码的。每个标签都有一个描述 - 如果您不确定,请阅读它。 -
忘了我可以自己改。感谢您的提示。
标签: java maven properties classpath maven-assembly-plugin