Search code examples
javaspringspring-bootnullpointerexceptionlog4j

Running Log4j2 with Java 11 and Spring Boot causes NullPointerException


When my Spring Boot application starts up and initializes my Log4j2 logger in Java 11, I get the following exception:

2021-10-08 11:20:29,949 main INFO Cannot initialize scripting support because this JRE does not support it. java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "javax.script.ScriptEngine.get(String)" is null
at org.luaj.vm2.script.LuaScriptEngineFactory.getParameter(Unknown Source)
at org.apache.logging.log4j.core.script.ScriptManager.<init>(ScriptManager.java:84)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:219)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:287)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:627)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:700)
at org.apache.logging.log4j.core.LoggerContext.setConfigLocation(LoggerContext.java:685)

There was another answer for the same problem a few years ago here: Log4j2/Slf4j and Java 11

However this answer from a few years ago, did not give me the steps I needed to figure out how to remove this. There was a discussion that it was an OpenJDK issue, and that it should be fixed now, but I am still struggling with this.

Does anyone understand this issue a bit more, why it only happens with Spring Boot, and what I can do to get rid of it in a clean way?


Solution

  • Your problem is different from the question you cite. Your classpath must contain the luaj-jse scripting engine, which has a bug:

        public Object getParameter(String key) {
            return getScriptEngine().get(key).toString();
        }
    

    When getParameter is called with a missing key (like "THREADING"), this causes a NullPointerException.

    You should report this bug to LuaJ. In the meantime you can either ignore the error or remove luaj-jse from your dependencies.