10

在我的 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 运行应用程序**

4

3 回答 3

20

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

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

否则你可以试试这个:

PropertyConfigurator.configure(this.getClass().getClassLoader().getResource("conf/log4j.properties"));
于 2012-05-24T03:04:47.420 回答
3

自动类将LogManager寻找一个名为log4j.propertiesor的文件log4j.xmlclasspath用于加载 log4j 类。直到 1.2.6 版本,log4j 只会log4j.properties在类路径中查找文件。从 1.2.7 版开始,log4j 在类路径中查找log4j.propertieslog4j.xml

只需将 log4j 文件放在默认包中。您还应该确保该文件位于.class.

于 2012-05-24T04:03:50.777 回答
1

我经常使用 Rachel Java 库http://rachel.sourceforge.net/来处理这类东西

下载 lib 并将其添加到您的类路径中。然后,您可以像这样使用它从 JAR 文件/包中加载任何文件,并且在使用 JNLP 部署的应用程序时它也可以工作。他们有很多很好的教程可供阅读,以了解加载文件的最佳方式。我通常喜欢将它们加载为输入流。

于 2012-05-24T04:49:07.927 回答