1

我知道这是一个陈词滥调的问题,我阅读了所有文件并尝试了许多解决方案,但始终没有解决方案对我有用!但是我将 log4j.properties 放在src/main/resourcessrc/main/java中,并且我已经添加PropertyConfigurator.configure("log4j.properties");到我的类中,但是当我将项目转换为Runnable JAR时,它向我显示:

C:\Users\eya\Desktop>java -jar RestCallAutomation.jar
log4j:WARN No appenders could be found for logger (com.infosquare.restautomation.AppController).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (Le fichier spécifié est introuvable)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:372)
        at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:403)
        at com.infosquare.restautomation.GenerateProcessors.start(GenerateProcessors.java:34)
        at com.infosquare.restautomation.AppController$2.run(AppController.java:98)
        at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        at java.lang.Thread.run(Unknown Source)
log4j:ERROR Ignoring configuration file [log4j.properties].

这是我项目的架构: maven project Architecture

这是 log4j.properties :

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

这是我的 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.OpenText</groupId>
  <artifactId>GenerateurCodes</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

  <dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-model</artifactId>
  <version>2.2.1</version>
</dependency>
  <dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom</artifactId>
    <version>1.1</version>
   </dependency>
 <dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1</version>
 </dependency>
 <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
    </dependency>
<dependency>
     <groupId>org.apache.velocity</groupId>
     <artifactId>velocity-tools</artifactId>
     <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.w3c</groupId>
    <artifactId>dom</artifactId>
    <version>2.3.0-jaxb-1.0.6</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

 </dependencies>
</project>

**更新 : **

当我使用 WinRar 打开 jar 时,它显示它包含 log4j.properties 和 log4j jar,所以这不是资源问题!

4

2 回答 2

1

首先:它称为:log4j.properties而不是 Log4j.properties !

第二:而不是PropertyConfigurator.configure("log4j.properties"); 它应该是:PropertyConfigurator.configure(getClass().getResource("/log4j.properties"));

于 2020-05-16T23:14:21.807 回答
0

检查 Maven 构建中的资源目录。它应该是这样的。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
.......
     <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
     </build>
</project>

这样 log4j.properties 文件将位于类路径目录中。

于 2020-05-16T22:06:37.167 回答