Using log4j2 from endorsed dirs

387 Views Asked by At

My example code is as simple as that:

import org.apache.log4j.Logger;
public class Test {
    public static void main(String args[]){
        LOG.info("information log line");
    }
    private final static Logger LOG = Logger.getLogger(Test.class);
}

It compiles and runs fine with log4j 2.11.0.

For a project that uses jacORB, I need to put the log4j jar files log4j-core-2.11.0.jar and log4j-api-2.11.0.jar into a directory that is added to the application via -Djava.endorsed.dirs=someDir. So, when I copy the log4j-jars into a subdirectory edDir and run the above example with

java -Djava.endorsed.dirs=edDir -Dlog4j.configurationFile=log4j.xml Test

It terminates with this output:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.apache.log4j.Logger.getLogger(Logger.java:41)
    at Test.<clinit>(Test.java:7)
Caused by: java.lang.NullPointerException
    at org.apache.logging.log4j.util.LoaderUtil.getClassLoaders(LoaderUtil.java:115)
    at org.apache.logging.log4j.util.ProviderUtil.<init>(ProviderUtil.java:66)
    at org.apache.logging.log4j.util.ProviderUtil.lazyInit(ProviderUtil.java:146)
    at org.apache.logging.log4j.util.ProviderUtil.hasProviders(ProviderUtil.java:130)
    at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:89)
    ... 2 more

It used to work with log4j 2.6. The error described above occured after upgrading to log4j 2.11.0.

Am I doing something wrong? How can I make this work again?

BTW, the JRE is 1.7.

3

There are 3 best solutions below

0
Bazinga On BEST ANSWER

The problem occured when log4j classes were loaded by the Bootstrap classloader, which is the case when the log4j jar files are found in the endorsed directory (-Djava.endorsed.dirs=...). Log4j didn't handle that correctly (see stacktrace in opening post).

The issue has been resolved in log4j 2.11.1.

0
Arnaud Jeansen On

Did you try with the command line parameter log4j.ignoreTCL=true

This parameter seems to be designed to look for the class on all classloaders.

So something like java -Djava.endorsed.dirs=edDir -Dlog4j.configurationFile=log4j.xml -Dlog4j.ignoreTCL=true Test

0
Bob On

Just built your project in eclipse using the example above (log4j-core-2.11.0.jar and log4j-api-2.11.0.jar) - you need to change your import statements and change Logger to LogManager:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;



public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LOG.info("information log line");
    }
    // private static final ExtLogger logger = ExtLogger.create(MyService.class);
    private final static Logger LOG = LogManager.getLogger(Test.class);
}