Log4j2/Slf4j 和 Java 11

问题描述 投票:0回答:2

我目前正在尝试使用 log4j/slf4j 和 java 11 构建一个应用程序,但我在运行时遇到这个问题:

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

此代码的起源是我的记录器的初始化:

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

我依赖于:

  • 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
  • 一个依赖org.slf4j:slf4j-api:1.8.0-beta2的maven项目

我使用的是 Java 版本的 OSX Mojave:

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)

有人知道我做错了什么以及如何解决这个问题吗?

java logging log4j2 slf4j java-11
2个回答
7
投票

根据Log4j文档

Log4j 提供对其某些组件中使用的 JSR 223 脚本语言的支持。

正如 Java 9 中

Alan Bateman
的评论所述,您必须使用
--add-modules java.scripting
或在
module-info.java
中显式添加 java 脚本模块。

另请注意,模块

jdk.scripting.nashorn
在 Java 11 中已弃用,并将在未来版本中删除。


0
投票

尝试升级到 log4j2 版本 2.17.2 或更高版本。

© www.soinside.com 2019 - 2024. All rights reserved.