Running Program with Log4j 2.4.1

Q

How to run my program with Log4j 2.4.1? My program uses Log4j 2 API only and I want to know which JAR file is needed to run it.

✍: FYIcenter.com

A

If your program is using Log4j 2 API, you need to run the program with JDK 1.7 or higher with 2 JAR files: the Log4j 2 API JAR and the Log4j 2 Core JAR, like log4j-api-2.4.1.jar and log4j-core-2.4.1.jar.

Here is a run session with the HelloLog4j2.java program:

fyicenter>java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)

fyicenter>javac -classpath \local\lib\log4j-api-2.4.1.jar 
   HelloLog4j2.java

fyicenter>java -classpath .;\local\lib\log4j-api-2.4.1.jar 
   HelloLog4j2
ERROR StatusLogger Log4j2 could not find a logging implementation. 
Please add log4j-core to the classpath. 
Using SimpleLogger to log to the console...
FATAL HelloLog4j2 Hello - fatal
ERROR HelloLog4j2 Hello - error

fyicenter>java -classpath .;\local\lib\log4j-api-2.4.1.jar;
   \local\lib\log4j-core-2.4.1.jar
   HelloLog4j2
ERROR StatusLogger No log4j2 configuration file found. 
Using default configuration: logging only errors to the console.
16:58:00.151 [main] FATAL HelloLog4j2 - Hello - fatal
16:58:00.152 [main] ERROR HelloLog4j2 - Hello - error   

The above session shows that:

  • The first command reports that we are using JDK 1.7.0.
  • The second command reports that compilation is seccussful.
  • The third command reports that execution partially fails, because log4j-core-2.4.1.jar is missing. But Log4j 2 API JAR is smart to using the SimpleLogger to write log messages to the console.
  • The fourth command reports that execution is successful with both log4j-api-2.4.1.jar and log4j-core-2.4.1.jar. Log messages appear on the console, because no configuration is provided.

Back to FAQ for Apache Log4j 2.4.1.

2015-11-11, 1266👍, 0💬