0

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

enter image description here

Stefanos
  • 37
  • 3
  • 11

2 Answers2

0

If you are using log4j 2.X, the configuration is different.

Justin
  • 407
  • 6
  • 16
  • Thank you for the reply. Please check my edit in the question – Stefanos Oct 04 '20 at 11:12
  • have you tried putting the config file in src/main/resources? Try it without using `PropertyConfigurator.configure("log4j.properties");` after you place the file in the aforementioned directory – Justin Oct 04 '20 at 11:15
  • It's already there. Check the picture I added in my edit – Stefanos Oct 04 '20 at 11:20
  • If that's not the case I'm not really sure what to do. Try [this](https://stackoverflow.com/questions/11302531/how-to-place-a-file-on-classpath-in-eclipse) link to see if it helps – Justin Oct 04 '20 at 11:31
0

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

Stefanos
  • 37
  • 3
  • 11