[英][TestNG][Jar] Set Classpath in Manifest (Class-Path) — classpath is ignored?
[英]Manifest/Class-Path in jar file not successfully setting a directory on the CLASSPATH for log4j2.xml
我已經成功編寫了一個小型Java程序,該程序可以從NetBeans運行時查找並使用我的log4j2.xml文件。 我現在正在嘗試創建一個包含其他jar的jar文件,因此可以在命令行上運行它。 我使用Ant構建了jar文件,並在jar目標中使用了“清單”。
構建之后,我執行“ java -jar BimbleParser.jar”,並且找到了放置在清單“ Class-Path”中的所有jar文件,程序正常運行。 除非它不會按log4j2.xml文件記錄(.log文件)。 當我進行設置以強制執行錯誤時,我在命令行中記錄了一些“錯誤”,我相信log4j2的默認行為,表明它未找到log4j2.xml文件。
我非常肯定在CLASSPATH中找不到包含log4j2.xml的目錄,該目錄是在構建文件的“ jar”目標中的清單中設置的。
這是其中包含清單的ant任務:
<target name="jar" description="Create jar file" depends="junit">
<jar destfile="${project.name}.jar" duplicate="add" basedir="${classes_dir}" filesetmanifest="skip" >
<manifest>
<attribute name="Build-Date" value="${TODAY} ${TSTAMP}"/>
<attribute name="Main-Class" value="com.mumble.parsers.Bumble.BumbleParser"/>
<attribute name="Class-Path" value="./CCom/config ./CCom/lib/CComCommons.jar ./CCom/lib/commons-configuration.jar ./CCom/lib/commons-lang.jar ./CCom/lib/log4j-api.jar ./CCom/lib/log4j-core.jar ./CCom/lib/commons-logging.jar ./CCom/lib/commons.lang3.jar"/>
</manifest>
</jar>
</target>
log4j2.xml位於第一個類路徑條目中,為“ ./CCom/config”,所有其他jar文件都已找到並由我的程序使用。 我究竟做錯了什么?
在這種“ java -jar BimbleParser.jar”場景中,也許還有其他解決方案可以通過類路徑顯示log4j2.xml文件?
謝謝你的幫助
我認為自動配置中仍然存在錯誤。 作為一種解決方法,請嘗試使用此方法(更正log2j.xml文件的路徑之后):
URI configuration = MyClass.class.getResource("/<path>/log4j2.xml").toURI();
Configurator.initialize("config", null, configuration);
基於示例: https : //svn.apache.org/repos/asf/logging/log4j/log4j2/trunk/core/src/test/java/org/apache/logging/log4j/FormatterLoggerManualExample.java
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.