英文:
log4j.properties file not found in project
问题
以下是翻译好的内容:
我在我的项目中使用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 Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (No such file or directory)
奇怪的是,如果我使用 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文件夹中。
英文:
I am using Log4j in my project and trying to run my application but it seems it cannot find log4.properties file.
This is my Maven configuration:
<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>
And this is how I am loading the log4j.properties file in my code.
PropertyConfigurator.configure("log4j.properties");
When I try to run the application I get the following error:
> log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (No such file or directory)
The weird thing is that if I package my application with mvn package and open the jar file I can see the log4j.properties file inside. I don't understand what is wrong since I declare the resources folder in the pom file.
EDIT: I am adding the dependency I use for log4j in the project
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
log4j.properties file is inside the src/main/resources folder
答案1
得分: 0
如果您正在使用log4j 2.X,配置是不同的。
英文:
If you are using log4j 2.X, the configuration is different.
答案2
得分: 0
这一行实际上是导致错误的原因:
PropertyConfigurator.configure("log4j.properties");
显然,只要在maven中设置了<directory>/src/main/resource</directory>
,并且log4j.properties文件存在于资源文件夹中,我就可以直接使用Logger,而不需要这一行。
英文:
It turns out that this line was causing the error
PropertyConfigurator.configure("log4j.properties");
Apparently as long as the <directory>/src/main/resource</directory>
is set in maven and the file log4j.properties file exists inside the resources folder I can use the Logger directly without this line
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论