【发布时间】:2021-01-19 10:40:59
【问题描述】:
我在我的项目中使用 Log4j 并尝试运行我的应用程序,但它似乎找不到 log4.properties 文件。
这是我的 Maven 配置:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>log4j.properties</include>
<include>hibernate.cfg.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<finalName>File-Name</finalName>
<archive>
<manifest>
<mainClass>my.main.class</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
这就是我在代码中加载 log4j.properties 文件的方式。
PropertyConfigurator.configure("log4j.properties");
当我尝试运行应用程序时,出现以下错误:
log4j:ERROR 无法读取配置文件 [log4j.properties]。 java.io.FileNotFoundException: log4j.properties(没有这样的文件或目录)
奇怪的是,如果我用 mvn package 打包我的应用程序并打开 jar 文件,我可以看到里面的 log4j.properties 文件。由于我在 pom 文件中声明了资源文件夹,因此我不明白出了什么问题。
编辑:我在项目中添加了我用于 log4j 的依赖项
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.properties 文件位于 src/main/resources 文件夹中
【问题讨论】: