NoSuchMethodError with SLF4J API

2020-03-01 04:20发布

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)

标签: java slf4j
7条回答
家丑人穷心不美
2楼-- · 2020-03-01 04:47

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

查看更多
贼婆χ
3楼-- · 2020-03-01 04:48

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.

查看更多
Juvenile、少年°
4楼-- · 2020-03-01 04:55

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.

查看更多
小情绪 Triste *
5楼-- · 2020-03-01 05:01

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>
查看更多
狗以群分
6楼-- · 2020-03-01 05:09

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.

查看更多
神经病院院长
7楼-- · 2020-03-01 05:10

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>
查看更多
登录 后发表回答