在 Intellij idea Ultimate2017.2 中运行 maven webapp 时出错:原因:java.lang.NoClassDefFoundError: Lorg/slf4j/Lo

Posted

技术标签:

【中文标题】在 Intellij idea Ultimate2017.2 中运行 maven webapp 时出错:原因:java.lang.NoClassDefFoundError: Lorg/slf4j/Logger【英文标题】:Error when run maven webapp in Intellij idea ultimate2017.2: Caused by: java.lang.NoClassDefFoundError: Lorg/slf4j/Logger 【发布时间】:2017-07-20 11:21:50 【问题描述】:

我将一个现有的maven项目导入Intellij idea Ultimate2017.2,并添加了一个tomcat本地服务器,但是当我运行该项目时,出现错误:

    Caused by: java.lang.NoClassDefFoundError: Lorg/slf4j/Logger;
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
    at java.lang.Class.getDeclaredFields(Class.java:1916)
    at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:110)
    at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:262)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationListenerAnnotations(WebAnnotationSet.java:88)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:64)
    at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:328)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:778)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:94)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5105)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 42 more
Caused by: java.lang.ClassNotFoundException: org.slf4j.Logger
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)

我正在使用tomcat8.5.16maven3.5.0,而pom.xml 中的slf4j 依赖项是:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>$slf4j.version</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>$slf4j.version</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>$slf4j.version</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>$slf4j.version</version>
    <scope>runtime</scope>
</dependency>

intellij 会自动将 maven 依赖添加到 web lib 中吗?在webapp/WEB_INF中找不到lib目录,如何将maven库添加到webapp中?

【问题讨论】:

尝试重新导入项目,IntelliJ IDEA 将使用所有库自动生成工件,请参阅jetbrains.com/help/idea/artifacts.html 【参考方案1】:

应该使用 mvn 命令来构建项目。打开Run/Debug Configuration -> 选择你创建的Tomcat Server->Before launch:Maven Goal,Build Artifacts,Activate tool window -> 点击+ 并使用maven clean package

【讨论】:

以上是关于在 Intellij idea Ultimate2017.2 中运行 maven webapp 时出错:原因:java.lang.NoClassDefFoundError: Lorg/slf4j/Lo的主要内容,如果未能解决你的问题,请参考以下文章

intellij idea 14.1.5 怎么弄中文

怎样在intellij idea 上开发struts应用

macOS&&IntelliJ IDEA-IntelliJ IDEA介绍以及IntelliJ IDEA 2019.3新特性

intellij idea中可以选择多行吗

intellij idea怎么安装插件

intellij idea 14单独编译class