java NoSuchMethodError: org/apache/log4j/Logger.setLevel(Lorg/apache/log4j/Level;)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31288525/
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
NoSuchMethodError: org/apache/log4j/Logger.setLevel(Lorg/apache/log4j/Level;)
提问by akcasoy
I am using these log4j, slf4j and ch.qos.logback dependencies in my pom.xml of my java project:
我在我的 java 项目的 pom.xml 中使用这些 log4j、slf4j 和 ch.qos.logback 依赖项:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
I recently wanted to use an external servlet library and defined that dependency in pom. The init method of this servlet looks like this:
我最近想使用外部 servlet 库并在 pom.xml 中定义该依赖项。该 servlet 的 init 方法如下所示:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
....
public class HealthpageServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private final Logger logger = Logger.getLogger(HealthpageServlet.class.getName());
protected HealthpageService service;
public static final String VERSION = "Healthpage-Version 0.0.4";
public void init() throws ServletException {
logger.setLevel(Level.INFO);
.....
}
My Weblogic Server shows me this exception from the init method, when i start the server:
当我启动服务器时,我的 Weblogic 服务器向我显示了 init 方法中的这个异常:
java.lang.NoSuchMethodError: org/apache/log4j/Logger.setLevel(Lorg/apache/log4j/Level;)V
java.lang.NoSuchMethodError: org/apache/log4j/Logger.setLevel(Lorg/apache/log4j/Level;)V
When i in eclipse open that HealthpageServlet jar content, it has a log4j-1.2.15 jar inside the WEB-INF/lib folder. But even if i exclude that dependency from pom.xml, i see this exception and my server does not start. What is the problem?
当我在 Eclipse 中打开 HealthpageServlet jar 内容时,它在 WEB-INF/lib 文件夹中有一个 log4j-1.2.15 jar。但即使我从 pom.xml 中排除了该依赖项,我也会看到此异常并且我的服务器无法启动。问题是什么?
采纳答案by akcasoy
Since the log4j Version (1.2.15) of my external jar was incompatible with the slf4j Version (1.6.4) of my parent project (where logger.setLevel() is not available), i had to update my slf4j to version 1.7.7.
由于我的外部 jar 的 log4j 版本 (1.2.15) 与我的父项目的 slf4j 版本 (1.6.4) 不兼容(其中 logger.setLevel() 不可用),我不得不将我的 slf4j 更新到版本 1.7。 7.
回答by Abhishek Singh
I know it's too late to answer but adding this answer which might help others. In my case, by trying different configuration I concluded that the log4j and slf4j to work successfully we need to find out the version match. The versions mismatch between spring-mvc and above two dependencies will result in the above error. So try to find out the right version match between 3 dependencies. I tried with below dependencies to get work done.
我知道现在回答为时已晚,但添加此答案可能对其他人有所帮助。就我而言,通过尝试不同的配置,我得出结论,log4j 和 slf4j 要成功运行,我们需要找出版本匹配。spring-mvc 和以上两个依赖的版本不匹配会导致上面的错误。因此,尝试找出3 个依赖项之间的正确版本匹配。我尝试使用以下依赖项来完成工作。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j13</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>