Fred_2
Fred_2

Reputation: 249

Log4j eclipse class not found

(OS: Windows 10)

Hi all,

In order, i

1- Downloaded the last version of log4j2, unzipped it in .../Program Files/Java folder

2- Added the jar files (api e core jars) to my project folder in eclipse and added them to the project build path.

3- Created the log4j.properties file with the following instructions, and added it to the build path

#TRACE < DEBUG < INFO < WARN < ERROR < FATAL
log4j.rootlogger=DEBUG, toConsole

#Console
log4j.appender.toConsole.org.apache.log4j.ConsoleAppender
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.toConsole.layout.ConversionPattern=%d[HH:mm:ss] %5p [%t] - %c.%M - %n%N

4- Wrote a simple code to test the library

import org.apache.log4j.Logger;

public class Log4j {

    private static final Logger log = Logger.getLogger(Log4j.class);

    public static void main(String[] args) {

        log.info("test");

    }
}

Once i run Log4j, i got this exception handler in eclipse console:

<terminated> Log4j[Java Application] C:\Program files\Java\jre1.8.0_161\bin\javaw.exe


Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/spi/LoggerContext
    at Log4j.<clinit>(Log4j.java:4)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.spi.LoggerContext
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

In the past days i have tried installing log4j several times with no success, just today i finally set up successfully the library, so i think that the problem could be about some collision with previous installations, even if i have checked also for these.

Thank you for the help.

Upvotes: 0

Views: 3593

Answers (2)

Try to have the dependency to the jar : log4j-api-2.17.1.jar

Upvotes: 0

Srinivas Lakshman
Srinivas Lakshman

Reputation: 489

You will have to get log4j adapter to use those methods. Try having log4j dependency/jar in the project and trying it out.

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.7</version>
</dependency>

Upvotes: 1

Related Questions