java - 从 java 中的包加载 log4j 属性

标签 java properties log4j

在我的 java swing 应用程序中,我从存储在应用程序包中的属性文件中加载 log4j 属性,并将该属性文件加载为,

try {                            
     PropertyConfigurator.configure("conf/log4j.properties");
     logger.info("Starting the system.");                           

} catch (Exception e) {
     e.printStackTrace();

}

然后在应用程序启动时出现以下错误,

log4j:ERROR Could not read configuration file [conf/log4j.properties].
java.io.FileNotFoundException: conf/log4j.properties (No such file or directory)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at java.io.FileInputStream.<init>(FileInputStream.java:97)
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:297)
        at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315)
        at com.bio.ofm.mnu.views.SplashScreen$1.run(SplashScreen.java:70)
        at java.lang.Thread.run(Thread.java:722)
log4j:ERROR Ignoring configuration file [conf/log4j.properties].
log4j:WARN No appenders could be found for logger (com.bio.ofm.mnu.views.SplashScreen).
log4j:WARN Please initialize the log4j system properly.

这种加载属性文件的方式是错误的吗?请帮忙。

我构建了一个 .jar 文件并使用该 jar 运行应用程序**

最佳答案

如果 conf 是您可以使用的源文件夹:

PropertyConfigurator.configure("classpath:conf/log4j.properties");

否则你可以试试这个:

PropertyConfigurator.configure(this.getClass().getClassLoader().getResource("conf/log4j.properties"));

关于java - 从 java 中的包加载 log4j 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10730415/

相关文章:

java - myClassInstance.property 和 myClassInsance.getProperty() 之间的区别?

Java 选项卡式 Pane : display an icon close on the title

java - 设计具有历史数据完整性的系统,同时存储实时事件

java - Tomcat - 从 JSP 访问属性

python - Python 的 property() 应该用作装饰器还是保存到变量中?

java - Tomcat 服务器中的 Log4j FileAppender 问题

java - 启动嵌入式jetty时指定系统属性

java - 如何让 Spring 为类上的同一个 validator 显示不同的基于验证包的消息?

ios - 当 Swift 中的变量值发生变化时执行方法

java - Log4J 不在日志文件条目之间添加换行符