Compiling Program with Log4j 2.4.1

Q

How to compile 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 compile it.

✍: FYIcenter.com

A

If your program is using Log4j 2 API, you need to compile the program with JDK 1.7 or higher with only the Log4j 2 API JAR, like log4j-api-2.4.1.jar.

Here is a compilation 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)
Java HotSpot(TM) Client VM (build 24.45-b08, mixed mode, sharing)

fyicenter>javac HelloLog4j2.java
HelloLog4j2.java:2: error: package org.apache.logging.log4j does not exist
import org.apache.logging.log4j.*;
^
HelloLog4j2.java:4: error: cannot find symbol
   private static final Logger logger
                        ^
  symbol:   class Logger
  location: class HelloLog4j2
HelloLog4j2.java:5: error: cannot find symbol
      = LogManager.getLogger("HelloLog4j2");
        ^
  symbol:   variable LogManager
  location: class HelloLog4j2
3 errors

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

The above session shows that:

  • The first command reports that we are using JDK 1.7.0.
  • The second command reports that compilation fails without Log4j 2 JAR file.
  • The third command reports that compilation is seccussful if log4j-api-2.4.1.jar is included in the classpath.

Back to FAQ for Apache Log4j 2.4.1.

2015-11-18, 1065👍, 0💬