log4.properties 应该在类路径上吗?
我在将 log4j.properties 文件放在类路径上时遇到一些问题。我在开发 (Eclipse Indigo) 时可以使用它,但是当我将应用程序导出为 JAR 时,我不能使用它。
我为导出的 JAR 手工制作了 MANIFEST.MF 文件:
Manifest-Version: 1.0
Main-Class: main.Program
Class-Path: lib/log4j.properties lib/log4j-1.2.15.jar
然后将 JAR 放在此文件组织中:
folder
|-------- app.jar
|-------- lib
|--------- log4j.properties
|--------- log4j-1.2.15.jar
当我尝试运行 app.jar 时,它们找到 log4j.jar 但找不到 log4j.properties:
log4j:WARN No appenders could be found for logger (main.Program).
log4j:WARN Please initialize the log4j system properly.
我的 log4j.properties 文件是这样的:
log4j.rootLogger=INFO, stdout, file
PATTERN=[%d] [%p] [%c{1}]: %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${PATTERN}
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=${logger_file_path}
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=${PATTERN}
I'm having some problems putting my log4j.properties file on classpath. I can use it when I'm developing (Eclipse Indigo) but, when I export my app as a JAR, I can't.
I've made by hand a MANIFEST.MF file for the exported JAR:
Manifest-Version: 1.0
Main-Class: main.Program
Class-Path: lib/log4j.properties lib/log4j-1.2.15.jar
And then with put the JAR on this file organization:
folder
|-------- app.jar
|-------- lib
|--------- log4j.properties
|--------- log4j-1.2.15.jar
When I try to run app.jar, they find log4j.jar but not log4j.properties:
log4j:WARN No appenders could be found for logger (main.Program).
log4j:WARN Please initialize the log4j system properly.
My log4j.properties file it's like this:
log4j.rootLogger=INFO, stdout, file
PATTERN=[%d] [%p] [%c{1}]: %m%n
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${PATTERN}
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=${logger_file_path}
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=${PATTERN}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另一种方法可以做到这一点:
-Dlog4j.configuration=文件:“./log4j.properties”
one more way to do this:
-Dlog4j.configuration=file:"./log4j.properties"
我知道的有三种方法
log4j.properties
添加到app.jar
log4j.properties
放入“文件夹”(JAR 所在的位置)并更改类 -.lib/log4j-1.2.15.jar
的路径。log4j.properties
放入名为“conf”的文件夹,并将类路径更改为./conf/
。我试过了,有效。
There are three ways l know.
log4j.properties
toapp.jar
log4j.properties
to the "folder" (where the JAR exists) and change the class-path to.lib/log4j-1.2.15.jar
.log4j.properties
to the folder named "conf" for example, and change the class-path to./conf/
.I have tried, it works.
不要将 log4j.properties 本身放在类路径中,而应将其放在包含该文件的目录中。
Do not put the
log4j.properties
itself in the classpath, but the directory that contains that file.将
log4j.properties
添加到app.jar
。Add
log4j.properties
toapp.jar
.我尝试了Jesper的答案,一开始不起作用。然后我尝试了一下
,之后就成功了。
I tried the answer of Jesper, and at first it didn't work. Then I tried with
And it worked after that.
尝试执行:
javar -jar -Dlog4j.configuration=file:"./log4j.properties" app.jar
try to execute:
javar -jar -Dlog4j.configuration=file:"./log4j.properties" app.jar