如何在 jar 清单类路径中指定属性文件的目录?
我想将可执行 jar 所需的所有 .properties 文件放在运行 jar 文件的目录之外的子目录中。目前,我的 MANIFEST.MF 看起来像这样:
Manifest-Version: 1.0
Build-Jdk: 1.6.0_26
Main-Class: MyMainClass
Class-Path: . conf lib/dependency1.jar
如果我将 .properties 文件保存在与 jar 文件相同的目录中,则一切正常。如果我将它们移至 conf
子目录,则找不到它们。我还尝试直接指定 .properties 文件的路径,例如 conf/log4j.properties
。
我想做的事情可能吗?
更新:以下是我的可执行 jar 中失败的代码示例:
InputStream stream = getClass().getClassLoader().getResourceAsStream("myproperties.properties");
当 myproperties.properties
位于 conf
子目录中时,此语句返回一个空的输入流。当 log4j.properties
位于 conf
子目录中时,它会输出有关找不到其配置的警告消息:
log4j:WARN No appenders could be found for logger (MyMainClass).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I'd like to put all of the .properties files that my executable jar requires in a sub-directory off of the directory from which I will run the jar file. Currently, my MANIFEST.MF looks something like this:
Manifest-Version: 1.0
Build-Jdk: 1.6.0_26
Main-Class: MyMainClass
Class-Path: . conf lib/dependency1.jar
If I keep my .properties files in the same directory as the jar file, everything works fine. If I move them to the conf
sub-directory, they are not found. I've also tried specifying the paths to the .properties files directly, such as conf/log4j.properties
.
Is what I'm trying to do possible?
Update: Here's an example of code in my executable jar that fails:
InputStream stream = getClass().getClassLoader().getResourceAsStream("myproperties.properties");
When myproperties.properties
is in the conf
sub-directory, this statement returns a null InputStream. When log4j.properties
is in the conf
sub-directory, it outputs its warning message about not finding its configuration:
log4j:WARN No appenders could be found for logger (MyMainClass).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 org.apache.log4j.PropertyConfigurator.configure(String configFilename)
这可用于指定日志文件所在的位置。
这里有一个例子:
http://logging.apache.org/log4j/1.2/manual.html
Try using
org.apache.log4j.PropertyConfigurator.configure(String configFilename)
This can be used to specify where your log file is located.
There is an example of that here:
http://logging.apache.org/log4j/1.2/manual.html