如何在jar文件外移动log4j.properties?

m-r*_*ric 5 java log4j gradle

jar使用gradle 构建一个文件,我的应用程序使用log4j.最初我的log4j.properties嵌入在我的jar文件中.我把它移到外面是为了能够在生产中修改它.我更新了META-INF/MANIFEST.MFjar文件中嵌入的文件以反映这一点:

Class-Path: lib/log4j-1.2.1 lib/slf4j-api-1.4.2.jar
            lib/slf4j-api-1.5.6.jar lib/slf4j-log4j12-1.5.6.jar
            lib/log4j-1.2.14.jar
            ...
            lib/log4j.properties
Run Code Online (Sandbox Code Playgroud)

我的filetree在windows7 32bit机器上如下:

root/myapp.jar
root/lib/log4j.properties
Run Code Online (Sandbox Code Playgroud)

我用这个命令启动我的应用程序:

java -Dlog4j.configuration='lib\log4j.properties' -Dlog4j.debug=true -jar myapp.jar > logs/output.log
Run Code Online (Sandbox Code Playgroud)

logs/output.log说log4j找不到它的属性文件:

log4j: Trying to find ['lib\log4j.properties'] using context classloader sun.misc.Launcher$AppClassLoader@17fa65e.
log4j: Trying to find ['lib\log4j.properties'] using sun.misc.Launcher$AppClassLoader@17fa65e class loader.
log4j: Trying to find ['lib\log4j.properties'] using ClassLoader.getSystemResource().
log4j: Could not find resource: ['lib\log4j.properties'].
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

-Dlog4j.configuration='lib/log4j.properties'
-Dlog4j.configuration='log4j.properties'
-Dlog4j.configuration=log4j.properties
Run Code Online (Sandbox Code Playgroud)

当我将文件嵌入jar中时,它可以工作:

log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@18385e3.
log4j: Using URL [jar:file:/C:/Users/User/Desktop/myapp/myapp.jar!/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL jar:file:/C:/Users/User/Desktop/myapp/myapp-2.1.9.jar!/log4j.properties
log4j: Parsing for [root] with value=[debug, A].
Run Code Online (Sandbox Code Playgroud)

但我需要在外面......任何想法?

wal*_*orn 5

尝试这个:

-Dlog4j.configuration=file:lib/log4j.properties
Run Code Online (Sandbox Code Playgroud)

但我很好奇:清单中的Class-Path定义确实有效吗?Jars,是的,这是标准配置,但是也可以用于log4j.properties吗?