Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2/Slf4j and Java 11

I'm currently trying to build an app with log4j/slf4j and java 11 but I'm facing this problem at runtime:

2018-12-10 22:09:27,225 main INFO Cannot initialize scripting support because this JRE does not support it. java.lang.NoClassDefFoundError: javax/script/ScriptEngineManager
    at [email protected]/org.apache.logging.log4j.core.script.ScriptManager.<init>(ScriptManager.java:69)
    at [email protected]/org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:216)
    at [email protected]/org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:250)
    at [email protected]/org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:547)
    at [email protected]/org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:619)
    at [email protected]/org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:636)
    at [email protected]/org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231)
    at [email protected]/org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
    at [email protected]/org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
    at org.apache.logging.log4j/org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.logging.log4j/org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:121)
    at [email protected]/org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:49)
    at org.apache.logging.log4j/org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
    at [email protected]/org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j/org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:355)
    at org.slf4j/org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:380)
    at fr.mrcraftcod.nameascreated/fr.mrcraftcod.nameascreated.NameAsCreated.<clinit>(NameAsCreated.java:39)
Caused by: java.lang.ClassNotFoundException: javax.script.ScriptEngineManager
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 17 more

The code that is at the origin of this is the initialization of my logger:

private static final Logger LOGGER = LoggerFactory.getLogger(NameAsCreated.class);

I have dependencies on:

  • org.apache.logging.log4j:log4j-api:2.11.1
  • org.apache.logging.log4j:log4j-core:2.11.1
  • org.apache.logging.log4j:log4j-slf4j18-impl:2.11.1
  • A maven project that depends on org.slf4j:slf4j-api:1.8.0-beta2

I'm on OSX Mojave with Java version:

openjdk version "11" 2018-09-25
OpenJDK Runtime Environment AdoptOpenJDK (build 11+28-201810021910)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11+28-201810021910, mixed mode)

Does someone have an idea of what I'm doing wrong and how I can solve this?

like image 789
RakSrinaNa Avatar asked Dec 10 '18 21:12

RakSrinaNa


People also ask

Is Log4j2 compatible with Java 11?

Everything works, except the specific file logger. Logging works fine on previous Java versions - 10, 9, 8, but not on Java 11.

Can we use Log4j2 with slf4j?

If you used Log4j2 with SLF4J, the only thing you need is replacing Log4j2 JAR files (or maven dependencies) by JDK logger Binding. You do not want to touch any source files in order to replace your logger.

What version of log4j does slf4j use?

The Log4j 2 Setup The latest version can be found here: log4j-api, log4j-core, log4j-slf4j-impl. The actual logging configuration adheres to native Log4j 2 configuration. Note that the Logger and LoggerFactory belong to the org. slf4j package.

Which version of slf4j is not vulnerable?

If you are using log4j-over-slf4j. jar in conjunction with the SLF4J API, you are safe unless the underlying implementation is log4j 2.


1 Answers

According to the Log4j documentation:

Log4j provides support for JSR 223 scripting languages to be used in some of its components.

and as said in the comments by Alan Bateman from Java 9 you have to add explicitly the java scripting module either with --add-modules java.scripting or in module-info.java.

Note also that the module jdk.scripting.nashorn is deprecated in Java 11 and will be removed in a future release.

like image 113
Ortomala Lokni Avatar answered Sep 21 '22 20:09

Ortomala Lokni