【问题标题】:How do I specify a directory for properties files in a jar manifest classpath?如何在 jar 清单类路径中指定属性文件的目录?
【发布时间】:2023-03-18 14:15:01
【问题描述】:

我想将我的可执行 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 子目录中时,该语句返回一个空的InputStream。当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.

【问题讨论】:

  • 谁在加载属性文件?可执行的 JAR?还是dependency1.jar?究竟如何?
  • 更新了问题,提供了有关失败的更多详细信息。
  • 您真的需要将它们放在不同的运行位置,还是更多地是将它们组织在开发层次结构中的不同源文件夹中?
  • 是的,这是关于运行时的位置。不过,与其说是需要,不如说是想要;)

标签: java jar classpath manifest


【解决方案1】:

尝试使用org.apache.log4j.PropertyConfigurator.configure(String configFilename)

这可用于指定您的日志文件所在的位置。

这里有一个例子: http://logging.apache.org/log4j/1.2/manual.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 2012-07-17
    • 2018-09-18
    • 2011-01-06
    • 2018-01-03
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多