NoSuchMethodError: org.slf4j [英] NoSuchMethodError: org.slf4j

查看:52
本文介绍了NoSuchMethodError: org.slf4j的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python 风暴.我用这个命令在本地运行拓扑

I'm using storm with python. I used this command to run the topology locally

mvn compile exec:java -Dexec.classpathScope=compile -Dexec.mainClass=my.Topology

并得到这个错误

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.apache.log4j.Category.differentiatedLog(Category.java:186)
at org.apache.log4j.Category.info(Category.java:229)

我用这个命令mvn dependency:tree查看slf4j的版本这是我得到的 slf4j 的一部分

I used this command mvn dependency:tree to see the versions of slf4j and here is the part of slf4j i got

org.apache.storm:storm-core:jar:0.9.6:provided
[INFO]    +- org.clojure:clojure:jar:1.5.1:provided
[INFO]    +- clj-time:clj-time:jar:0.4.1:provided
[INFO]    +- joda-time:joda-time:jar:2.0:provided
[INFO]    +- compojure:compojure:jar:1.1.3:provided
[INFO]    +- org.clojure:core.incubator:jar:0.1.0:provided
[INFO]    +- org.clojure:tools.macro:jar:0.1.0:provided
[INFO]    +- clout:clout:jar:1.0.1:provided
[INFO]    +- ring:ring-core:jar:1.1.5:provided
[INFO]    +- commons-fileupload:commons-fileupload:jar:1.2.1:provided
[INFO]    +- javax.servlet:servlet-api:jar:2.5:provided
[INFO]    +- hiccup:hiccup:jar:0.3.6:provided
[INFO]    +- ring:ring-devel:jar:0.3.11:provided
[INFO]    +- clj-stacktrace:clj-stacktrace:jar:0.2.2:provided
[INFO]    +- ring:ring-jetty-adapter:jar:0.3.11:provided
[INFO]    +- ring:ring-servlet:jar:0.3.11:provided
[INFO]    +- org.mortbay.jetty:jetty:jar:6.1.26:provided
[INFO]    +- org.mortbay.jetty:jetty-util:jar:6.1.26:provided
[INFO]    +- org.clojure:tools.logging:jar:0.2.3:provided
[INFO]    +- org.clojure:math.numeric-tower:jar:0.0.1:provided
[INFO]    +- org.clojure:tools.cli:jar:0.2.4:provided
[INFO]    +- commons-io:commons-io:jar:2.4:provided
[INFO]    +- org.apache.commons:commons-exec:jar:1.1:provided
[INFO]    +- commons-lang:commons-lang:jar:2.5:provided
[INFO]    +- com.googlecode.json-simple:json-simple:jar:1.1:provided
[INFO]    +- com.twitter:carbonite:jar:1.4.0:provided
[INFO]    +- com.esotericsoftware.kryo:kryo:jar:2.21:provided
[INFO]    +- 
  com.esotericsoftware.reflectasm:reflectasm:jar:shaded:1.07:provided
[INFO]    +- org.ow2.asm:asm:jar:4.0:provided
[INFO]    +- com.esotericsoftware.minlog:minlog:jar:1.2:provided
[INFO]    +- org.objenesis:objenesis:jar:1.2:provided
[INFO]    +- com.twitter:chill-java:jar:0.3.5:provided
[INFO]    +- org.yaml:snakeyaml:jar:1.11:provided
[INFO]    +- commons-logging:commons-logging:jar:1.1.3:provided
[INFO]    +- commons-codec:commons-codec:jar:1.6:provided
[INFO]    +- com.googlecode.disruptor:disruptor:jar:2.10.4:provided
[INFO]    +- org.jgrapht:jgrapht-core:jar:0.9.0:provided
[INFO]    +- ch.qos.logback:logback-classic:jar:1.0.13:provided
[INFO]    +- ch.qos.logback:logback-core:jar:1.0.13:provided
[INFO]    +- org.slf4j:slf4j-api:jar:1.7.5:provided
[INFO]    +- org.slf4j:log4j-over-slf4j:jar:1.6.6:provided
[INFO]    \- jline:jline:jar:2.11:provided

我的 POM

<project xmlns="http://maven.apache.org/POM/4.0.0" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Sim</groupId>
<artifactId>Project</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Pro</name>
<url>http://maven.apache.org</url>
<dependencies>
  <dependency>
    <groupId>org.apache.storm</groupId>
    <artifactId>storm-core</artifactId>
    <version>0.9.6</version>
    <scope>provided</scope>
  </dependency>
  </dependencies>
  <build>
    <resources>
      <resource>
        <directory> ${basedir}/multilang</directory>
      </resource>
    </resources>
<plugins>
 <plugin>
  <artifactId>maven-assembly-plugin</artifactId>
   <configuration>
     <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
             <groupId>org.codehaus.mojo</groupId>
             <artifactId>exec-maven-plugin</artifactId>
             <version>1.5.0</version>
             <executions>
                 <execution>
                     <goals>
                        <goal>exec</goal>
                     </goals>
                 </execution>
             </executions>
            <configuration>
                <executable>java</executable>
  <includeProjectDependencies>true</includeProjectDependencies>
  <includePluginDependencies>true</includePluginDependencies>
  <classpathScope>compile</classpathScope>                                  
  <mainClass>Sim.Topology</mainClass>
                          </configuration>
                    </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
                            <version>2.3</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>

推荐答案

已编辑答案

我第一次怀疑这是提供或编译范围依赖的问题是错误的,因为您正在使用 classpathScope=compile 启动程序.

I was wrong with my first suspicion that this is a problem of provided or compile scope dependencies, as you are starting the program with classpathScope=compile.

找到了 slf4 api,否则错误消息将是 ClassNotFoundError.

The slf4 api is found, otherwise the error message would have been a ClassNotFoundError.

问题是log4j要调用如下方法:

The problem is that log4j wants to call the following method:

void log(Marker marker, String fqcn, int level, String message, Object[] argArray, Throwable t)

在 1.7.5 版的 slf4j api 中定义(根据 Javadoc,它从 1.3 版开始就在那里).

which is defined in the slf4j api in version 1.7.5 (it's in there since version 1.3 according to the Javadocs).

此错误的唯一解释是您的类路径上必须有另一个版本的 slf4j-api.jar 还没有此方法并且在加载 LocationAwareLogger 接口时使用.

The only explanation for this error is that there must be another version of slf4j-api.jar on your classpath which does not yet have this method and which is used when loading the LocationAwareLogger interface.

请检查

mvn dependency:tree

对于其他出现的 slf4j-api,可能隐藏在树中的某处.

for other occurrences of slf4j-api, perhaps hidden somewhere in the tree.

另一种找到类从何处加载的可能性是使用以下代码(取自这个问题):

Another possibility to find where the class is loaded from is by using the following code (taking from this question):

    Class clazz = Class.forName("org.slf4j.spi.LocationAwareLogger");
    URL resourceUrl = clazz.getResource("/" + clazz.getCanonicalName().replace(".", "/") + ".class");
    System.out.println(resourceUrl.toString());

更新:

所以当使用 mvn exec 运行程序时,有一个 slf4j-api 加载了 1.5.6 版本(欢迎来到 maven 依赖地狱).你能展示你项目的完整 pom.xml 吗?它是否有父 pom 或以其他方式引入的依赖项?如果您通过添加以下内容显式添加对所需版本的 slf4j-api 的依赖,会发生什么情况:

So when running the program with mvn exec there is a slf4j-api loaded with version 1.5.6 (welcome to the maven dependency hell). Can you show the complete pom.xml of your project? Does it have a parent pom or otherwise introduced dependencies? What happens if you explicitly add a dependency on the required version of slf4j-api by adding:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.9</version>
</dependency>

更新:

问题来自exec-maven-plugin,这是加载旧版本的slf4j-api.当您从插件的配置中删除以下行时:

The problem comes from the exec-maven-plugin, this is loading the older version of slf4j-api. When you remove the following line from the configuration of the plugin:

<includePluginDependencies>true</includePluginDependencies>

然后加载实际的 slf4j-api.您也可以将该值设置为 false,但无论如何这是默认值.我认为您不需要包含插件依赖项,在构建项目包时您也没有它们.此参数的文档说明:

then the actual slf4j-api is loaded. You could as well set the value to false, but that's the default value anyway. I do not think that you should need to include the plugin dependencies, when building the package of your project you do not have them either. The documentation for this parameter states:

指示在执行主类时是否应使用此插件的依赖项.当项目依赖项不合适时,这很有用.当项目不是 Java 项目时,仅使用插件依赖项会特别有用.例如,使用 csharp 插件的 mvn 项目只希望将 dotnet 库视为依赖项.

Indicates if this plugin's dependencies should be used when executing the main class. This is useful when project dependencies are not appropriate. Using only the plugin dependencies can be particularly useful when the project is not a java project. For example a mvn project using the csharp plugins only expects to see dotnet libraries as dependencies.

所以我认为删除这一行或将值设置为 false 将解决您的问题.

So I think that removing this line or setting the value to false will solve your problem.

这篇关于NoSuchMethodError: org.slf4j的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆