简体   繁体   English

如何在jar清单类路径中为属性文件指定目录?

[英]How do I specify a directory for properties files in a jar manifest classpath?

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. 我想将我的可执行jar所需的所有.properties文件放在要运行jar文件的目录的子目录中。 Currently, my MANIFEST.MF looks something like this: 当前,我的MANIFEST.MF看起来像这样:

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. 如果我将.properties文件与jar文件放在同一目录中,则一切正常。 If I move them to the conf sub-directory, they are not found. 如果将它们移至conf子目录,则找不到它们。 I've also tried specifying the paths to the .properties files directly, such as conf/log4j.properties . 我也尝试过直接指定.properties文件的路径,例如conf/log4j.properties

Is what I'm trying to do possible? 我想做的事可能吗?

Update: Here's an example of code in my executable jar that fails: 更新:这是我的可执行jar中失败的代码示例:

InputStream stream = getClass().getClassLoader().getResourceAsStream("myproperties.properties");

When myproperties.properties is in the conf sub-directory, this statement returns a null InputStream. myproperties.properties位于conf子目录中时,此语句返回空的InputStream。 When log4j.properties is in the conf sub-directory, it outputs its warning message about not finding its configuration: 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.

Try using org.apache.log4j.PropertyConfigurator.configure(String configFilename) 尝试使用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 还有就是一个很好的例子在这里: http://logging.apache.org/log4j/1.2/manual.html

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

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