Java 为什么我在运行 Jetty 时在 LocationAwareLogger 上不断收到 NoSuchMethodError?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19071485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:52:35  来源:igfitidea点击:

Why do I keep getting NoSuchMethodError on LocationAwareLogger when running Jetty?

javamavenjettyslf4j

提问by Psycho Punch

I'm trying to embed Jetty server into my automated tests so I've added the following dependency in my project:

我正在尝试将 Jetty 服务器嵌入到我的自动化测试中,因此我在我的项目中添加了以下依赖项:

<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-servlet</artifactId>
  <version>7.6.13.v20130916</version>
  <scope>test</scope>
</dependency>

I'm using Jetty 7 because the Web app uses Java 6, Servlet 2.5.

我使用 Jetty 7,因为 Web 应用程序使用 Java 6、Servlet 2.5。

However when I try to start embedded Jetty server, I get:

但是,当我尝试启动嵌入式 Jetty 服务器时,我得到:

java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
    at org.eclipse.jetty.util.log.JettyAwareLogger.log(JettyAwareLogger.java:607)
    at org.eclipse.jetty.util.log.JettyAwareLogger.warn(JettyAwareLogger.java:431)
    at org.eclipse.jetty.util.log.Slf4jLog.warn(Slf4jLog.java:69)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.setFailed(AbstractLifeCycle.java:204)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:74)

I've been trying to figure out what's wrong but none of the steps I've taken so far addresses the issue. Here's a list of what I've done so far:

我一直试图找出问题所在,但到目前为止我采取的步骤都没有解决这个问题。以下是我迄今为止所做的工作列表:

  • Trace transitive dependencies to search for incompatible versions of slf4j-api
  • Try out different versions of slf4j-api(1.5.11, 1.6.1, 1.6.4, 1.7.5)
  • Looked into the some sources of Jetty 7 particularly the pom.xml and found that there's a dependency to slf4j 1.6.1
  • Looked into this similar questionand got nullwhen trying to print org.slf4j.spi.LocationAwareLogger, and org.slf4j.Marker
  • 跟踪传递依赖以搜索不兼容的版本 slf4j-api
  • 尝试不同版本的slf4j-api(1.5.11, 1.6.1, 1.6.4, 1.7.5)
  • 查看了 Jetty 7 的一些来源,特别是 pom.xml,发现对 slf4j 1.6.1 有依赖性
  • 研究了这个类似的问题null在尝试打印时得到了org.slf4j.spi.LocationAwareLogger,并且org.slf4j.Marker

I hope someone can offer a fresh insight into this issue. Thanks.

我希望有人可以对这个问题提供新的见解。谢谢。

采纳答案by madhead

I bet you've faced a compatibility problembetween sl4jjars. Try examinating mvn dependency:treeoutput of your final artifact. Maybe you have two different versions of some sl4jjar and you cannot predict which loads first. Try to equalize all sl4jversions.

我敢打赌你遇到了jars之间的兼容性问题sl4j。尝试检查mvn dependency:tree最终工件的输出。也许您有一些sl4jjar 的两个不同版本,并且您无法预测首先加载哪个版本。尝试均衡所有sl4j版本。



OP: Please refer to the documentation on how I resolved this.

OP:请参阅有关我如何解决此问题文档

回答by Psycho Punch

The information on madhead's answerwas very helpful but I'm going to document the details of what I've done for the sake for others who (will) encounter the same problem.

关于madhead回答的信息非常有帮助,但为了(将)遇到相同问题的其他人,我将记录我所做工作的详细信息。

As the answer mentioned suggested, it was a problem with slf4jjars. To verify I did the following:

正如提到的答案所暗示的那样,这是slf4j罐子的问题。为了验证我做了以下事情:

  1. Define exclusion on all dependencies that have dependencies on slf4j.
  2. Follow option 2 from this suggestion.
    • I wrote a unit test that just sysout's a String of my choosing, and define a breakpoint at that line.
    • Then I debug the test as JUnit test, and open the Display view
    • I needed to re-run it because Display Result of Evaluating Selected Textwas disabled at first for some reason.
    • I found out that the runtime still refers to the 1.5.8 version of slf4jthat was still being added into the classpath regardless if I specified exclusion for it.
  3. The project I'm working on depends on another project I'm also working on (it's a multi-Maven project project), which specified dependency to slf4j-jdk14 1.5.8, so I just updated it to 1.6.1.
    • I had to specify exclusion of slf4j-apiunder slf4j-jdk14 1.6.1because it was referring to version 1.5.8, and add direct dependency to slf4j-api 1.6.1.
  1. 对所有依赖于 的依赖定义排除slf4j
  2. 遵循此建议中的选项 2 。
    • 我写了一个单元测试,它只是 sysout 是我选择的一个字符串,并在该行定义了一个断点。
    • 然后我将测试调试为 JUnit 测试,并打开显示视图
    • 我需要重新运行它,因为Display Result of Evaluating Selected Text一开始由于某种原因被禁用了。
    • 我发现运行时仍然指的是 1.5.8 版本,slf4j它仍然被添加到类路径中,无论我是否为它指定了排除。
  3. 我正在处理的项目依赖于我也在处理的另一个项目(它是一个多 Maven 项目项目),它指定了对 的依赖slf4j-jdk14 1.5.8,所以我只是将它更新为1.6.1.
    • 我必须指定排除slf4j-apiunder,slf4j-jdk14 1.6.1因为它指的是 1.5.8 版,并将直接依赖添加到slf4j-api 1.6.1.