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

发布于 2024-12-15 10:49:50 字数 961 浏览 4 评论 0原文

我想将可执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-12-22 10:49:50

尝试使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文