LocationAwareLogger上的SLF4J NoSuchMethodError

时间:2011-11-24 05:28:55

标签: java maven jar dependencies slf4j

这是一个之前被问过的问题,但不幸的是,似乎没有任何解决方案对我有用。我正面临这个异常(使用精简堆栈跟踪):

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.commons.logging.impl.SLF4JLocationAwareLog.debug(SLF4JLocationAwareLog.java:133)
    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(ThreadSafeClientConnManager.java:221)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)

使用命令行Maven进行编译时以及部署到Tomcat时会发生这种情况。它在IntelliJ IDEA中运行良好。

通常我希望这是由正在使用的SLF4J库的多个版本引起的。但Maven依赖树在单个版本中显示所有slf4j库:

..$ mvn dependency:tree | grep slf4j
[INFO] +- org.slf4j:slf4j-simple:jar:1.6.4:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.6.4:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.6.4:compile

我甚至确保〜/ .m2 / repository

中没有其他JAR

没有对commons-logging库的引用(我将它们全部排除在依赖树之外。

如何解决此问题?我的想法已经不多了。

编辑:这里要求完整的依赖关系,首先是父POM:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${org.hibernate.validator.version}</version>
            <exclusions>
                <!-- Exclude SLF4j to avoid version conflicts (we have 1.6.2, this drags in 1.6.1) -->
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator-annotation-processor</artifactId>
            <version>${org.hibernate.validator.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>${org.slf4j.backend}</artifactId>
            <version>${org.slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time-jsptags</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.0-801.jdbc4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
</dependencyManagement>

然后失败的模块:

<dependencies>
    // client specific dependencies skipped //

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>10.0.1</version>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>

    <!-- Logging -->
    <dependency>
        <groupId>net.sf.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>${org.slf4j.backend}</artifactId>
        <version>${org.slf4j.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>

    <!-- Joda Time -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time-jsptags</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.visualization</groupId>
        <artifactId>visualization-datasource</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.0</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.7</version>
    </dependency>

    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <scope>test</scope>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
</dependencies>

这些是在父级上设置的属性:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.hibernate.validator.version>4.2.0.Final</org.hibernate.validator.version>
    <org.slf4j.backend>slf4j-simple</org.slf4j.backend>
    <org.slf4j.version>1.6.4</org.slf4j.version>
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>

鉴于我最近的实验,它似乎并不是与项目文件有关的问题。我试图'hg bisect'这个问题,但回去几周我找不到一个有效的版本。其中一些在生产系统中运行,因此不太可能是导致此问题的代码更改。

8 个答案:

答案 0 :(得分:23)

NoSuchMethodError的javadoc说,

  

通常,编译器会捕获此错误;这个错误可以    只有在类的定义发生不兼容的情况下才会在运行时发生。

所以这可能是由于加载的slf4j版本不兼容造成的。而不是查看类路径并猜测加载类的位置,而不是按照here所述的方式查找类的加载位置。

打印出加载org.slf4j.spi.LocationAwareLoggerorg.apache.commons.logging.impl.SLF4JLocationAwareLogorg.slf4j.Marker的位置。

答案 1 :(得分:1)

一种解决方案。

验证eclipse目录:configuration \ org.eclipse.equinox.simpleconfigurator \ bundles.info

slf4j没有更多

答案 2 :(得分:1)

mvn clean dependency:tree -DskipTests;

删除“org.slf4j”的所有依赖项,除了一个(最高的一个“)

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

答案 3 :(得分:1)

我们遇到了类似的问题,结果发现我们在类路径中有两个不兼容的slf4j jar版本。 类路径具有以下2个不兼容的版本。从类路径中删除较低版本后,问题得到解决。

slf4j-api-1.6.1.jar
SLF4J-log4j12-1.6.1.jar

slf4j-api-1.5.11.jar
slf4j-log4j12-1.5.11.jar

答案 4 :(得分:1)

我有相同的错误消息,但是解决方案对我来说是不同的。我必须删除,以摆脱来自maven pom的依赖:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.6.1</version>
</dependency>

此后错误消失,一切对我都有用。

答案 5 :(得分:0)

我的pom文件中有石英调度程序,其中包括slf4j,所以我将其排除在外:

<exclusion>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
</exclusion>

并且工作了!

答案 6 :(得分:0)

解决了!!!

我依赖于另一个使用JavaDoc插件的项目。 JavaDoc插件内部使用Maven-coreMaven-core-2.2.1使用jcl-over-slf4j: 1.5.6Maven-core是父级jar。

现在,由于这个JCL jar,我正面临着这个问题。 因此,我将其从lib weblogic文件夹(或您可能使用的任何服务器)中删除。

和ALAS !!问题解决了。

注1: - 您还可以使用Maven的<exclusion>标记从JavaDoc(或任何其他插件)中删除此依赖关系以解决此问题。

注2: - 使用POM中的Dependency Hierarchy标签查看是否存在任何此类旧SLF4J个罐子。并删除休息,只保留一个版本。

希望有所帮助......

答案 7 :(得分:0)

当您具有使用相同传递依赖项的依赖项时,通常会发生这种情况。这意味着您的2个依赖项(或您的应用程序和传递依赖项)都在内部使用不同版本的SLF4J。但是你的应用程序只能同时使用该类的单个版本,所以它必须选择(不知道这里的规则......随机?)

要解决此问题,您通常需要执行mvn dependency:tree以查看哪些使用不同版本的SLF4J。

添加此依赖项解决了我的问题

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

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