SLF4J API 的 NoSuchMethodError

发布于 2024-10-28 01:38:26 字数 356 浏览 5 评论 0原文

与slf4j一起使用时,

String test = blahblahblah;
logger.info("{}",test);

Trace如下

java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;

at org.slf4j.impl.JDK14LoggerAdapter.info(JDK14LoggerAdapter.java:304)

When Use with slf4j,

String test = blahblahblah;
logger.info("{}",test);

Trace as below

java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;

at org.slf4j.impl.JDK14LoggerAdapter.info(JDK14LoggerAdapter.java:304)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

满栀 2024-11-04 01:38:26

看起来各种 SLF4J API 和集成库之间的版本不匹配。 SLF4J 在版本兼容性方面非常不稳定(例如 1.6.x 不向后兼容 1.5.x)。

确保各个 JAR 版本匹配,并确保类路径上没有重复的 JAR。

Looks like you have a version mis-match between the various SLF4J API and integration libraries. SLF4J is extremely twitchy when it comes to version compatibility (e.g. 1.6.x is not backwards compatible with 1.5.x).

Make sure the various JAR versions match, and make sure there are no duplicate JARs on the classpath.

装纯掩盖桑 2024-11-04 01:38:26

我收到此错误:

SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
.
.
.

现在我只是用 pom.xml 中的版本注释了行,如下所示,它现在正在工作:

    <dependency> 
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <!-- <version>1.6.5</version> -->
    </dependency>

I was getting this error:

SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6]
SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Lorg/slf4j/helpers/FormattingTuple;
.
.
.

Now I just commented line with version from pom.xml, as shown below, and it is working now:

    <dependency> 
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <!-- <version>1.6.5</version> -->
    </dependency>
海风掠过北极光 2024-11-04 01:38:26

另请确保您是否在外部(而非本地)中部署 Glassfish,以删除该服务器上 Glassfish 安装的 lib 文件夹中的重复依赖项。

就我而言,一切在本地运行良好,但一旦部署到服务器上,我就会收到此错误。

Also make sure if you deploy in Glassfish externally (not locally) to remove duplicate dependencies in the lib folder of the Glassfish installation on that server.

In my case, everything worked fine locally but once deployed on a server I got this error.

掩饰不了的爱 2024-11-04 01:38:26

这看起来您的 MessageFormatter 类的版本与 JDK14LoggerAdapter 类的版本不同。控制你的类路径。

This looks like you have a different version of the MessageFormatter class than your JDK14LoggerAdapter class. Control your classpath.

难得心□动 2024-11-04 01:38:26

我预计这是因为版本不兼容,例如(如果您正在运行应用程序 6.0 并持有 jar 文件(slf4j 1.5)或同时持有两者(slf4j 1.5 和 1.6)),则可能会引发异常。

建议是
寻找合适的版本
不要在构建路径中放置多个版本文件(slf4f 1.5 和 slf4j 1.6)文件,删除相应的版本文件

然后运行确定,您就会得到它。

I expect that this is because of uncompatible version, like (if you are running your application 6.0 and holding a jar file (slf4j 1.5) or holding both (slf4j 1.5 and 1.6)) then exception might raised.

suggestion is
go for proper version
dont place more than one version file (slf4f 1.5 and slf4j 1.6) file in the build path, delete the appropriate one

and

then run sure, you will get it.

夏有森光若流苏 2024-11-04 01:38:26

就我而言,我们在各种 SLF4J API 和集成库之间拥有正确的版本匹配。但我们也使用 tika-app jar,其中也包含 SLF4J 类。

要检查您是否还有一些包含 SLF4J 类的 (fat)jar,在 Unix 系统上:

转到 WEB-INF/lib/ 目录并运行以下命令

for i in *.jar;做 jar -tvf "$i" | grep -Hsi MessageFormatter &&回显“$i”;完成

这将在控制台上打印所有 jar 的匹配结果。

最后,我们用 tika-core jar 替换了 tika-app jar。

In my case, we are having correct version match between the various SLF4J API and integration libraries. But we are also using tika-app jar, which also have SLF4J classes wrapped inside it.

To check if you are also having some (fat)jar which contains SLF4J classes, On Unix system:

Go to your WEB-INF/lib/ directory and run following command

for i in *.jar; do jar -tvf "$i" | grep -Hsi MessageFormatter && echo "$i"; done

This will print matching result from all jars on console.

Finally, we replaced tika-app jar by tika-core jar.

给不了的爱 2024-11-04 01:38:26

就我而言,将 EAR 文件部署到 JBoss EAP 5.2 时,我遇到了相同的“java.lang.NoSuchMethodError”。

日志显示:

SLF4J:您的 slf4j 绑定请求的版本 1.5.6 与 [1.6] 不兼容

日志: (server.log)

2018-11-05 09:59:46,382 ERROR [STDERR] (main) SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
2018-11-05 09:59:46,395 ERROR [STDERR] (main) SLF4J: The requested version 1.5.6 by your slf4j binding is not compatible with [1.6]
2018-11-05 09:59:46,395 ERROR [STDERR] (main) SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
2018-11-05 09:59:46,402 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/common-scheduling/services]] (main) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at  

问题:

问题是类路径上有两个不同版本的 slf4j-api jar。 (slf4j-log4j12-1.5.6.jar、slf4j-api-1.6.1.jar)

解决方案:

  1. 我运行“mvn dependency:tree”来查找传递依赖项的位置。
  2. 然后,我添加了一个 Maven 排除标记来排除不需要的工件版本。
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.5.6</version>
  <exclusions>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </exclusion>
  </exclusions>
</dependency>

In my case, I was getting the same "java.lang.NoSuchMethodError" deploying an EAR file to JBoss EAP 5.2.

The logs showed:

SLF4J: The requested version 1.5.6 by your slf4j binding is not compatible with [1.6]

Logs: (server.log)

2018-11-05 09:59:46,382 ERROR [STDERR] (main) SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
2018-11-05 09:59:46,395 ERROR [STDERR] (main) SLF4J: The requested version 1.5.6 by your slf4j binding is not compatible with [1.6]
2018-11-05 09:59:46,395 ERROR [STDERR] (main) SLF4J: See http://www.slf4j.org/codes.html#version_mismatch for further details.
2018-11-05 09:59:46,402 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/common-scheduling/services]] (main) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.slf4j.helpers.MessageFormatter.format(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at  

Problem:

The problem was having two different versions of slf4j-api jars on the classpath. (slf4j-log4j12-1.5.6.jar, slf4j-api-1.6.1.jar)

Resolution:

  1. I ran "mvn dependency:tree" to find the location of the transitive dependency.
  2. Then, I added a maven exclusion tag to exclude the undesired artifact version.
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.5.6</version>
  <exclusions>
    <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </exclusion>
  </exclusions>
</dependency>
暖心男生 2024-11-04 01:38:26

我在 Spark 应用程序中遇到了同样的问题。
这是不同版本混合造成的。

我们应该使用这样的东西:

 "org.slf4j" % "slf4j-log4j12" % "1.7.30" % Test,
 "org.slf4j" % "slf4j-api" % "1.7.30" % Test,

I faced the same problem with Spark Application.
It is caused when different versions are mixed.

We should use something like this :

 "org.slf4j" % "slf4j-log4j12" % "1.7.30" % Test,
 "org.slf4j" % "slf4j-api" % "1.7.30" % Test,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文