【问题标题】:How to fix NoClassDefFoundError: org.slf4j.LoggerFactory in intelliJ (but eclipse works fine)如何修复 IntelliJ 中的 NoClassDefFoundError: org.slf4j.LoggerFactory (但 Eclipse 工作正常)
【发布时间】:2019-05-12 17:49:51
【问题描述】:

Eclipse 可以正常运行我的代码(右键单击 -> 运行),但是当我尝试在 intellij 中运行相同的项目和 main 方法时,出现此错误。有没有办法查看 eclipse 从哪里获取依赖项并在 Intellij 中复制添加这些依赖项?

我尝试将 API 从提供的切换到运行时......然后我遇到了一个不同的问题 - 无法找到正确的 LoggerFactory 类型。但是 eclipse 发现依赖关系很好。

这些在我的 pom 层次结构中:

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.11.1</version>
</dependency>

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

    Exception in thread "main" java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
at com.railroad.messaging.consoleapp.PumpEmpMessagetoQpid.<clinit>(PumpEmpMessagetoQpid.java:16)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:609)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:926)
at java.lang.ClassLoader.loadClass(ClassLoader.java:871)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:343)
at java.lang.ClassLoader.loadClass(ClassLoader.java:854)
... 1 more

【问题讨论】:

  • idea中有一个刷新/重建maven项目的选项。您会在右侧找到一个选项卡,它是 maven。点击maven,你会发现一个刷新图标。只需点击它。
  • 这并不能解决问题。我认为IDE需要在运行时定位Logger实现...
  • 我在 Intellij 中的 Spring Boot 应用程序也有同样的问题,eclipse 运行良好,编译后的一切运行良好。就在尝试从 intellij 运行它时,如果没有我明确添加这些依赖项,它将无法运行: org.slf4jslf4j-apiorg.yamlsnakeyaml

标签: java eclipse intellij-idea slf4j


【解决方案1】:

检查您的父项目 pom.xml。可能您需要删除 slf4j 依赖块中“提供”的范围(在 PARENT 项目中)。

<dependency>
    <groupId>org.slf4j</groupId
    <artifactId>slf4j-api</artifactId>
    <version>${slf4.version}</version>        

    <!-- DELETE THIS ROW: -->
    <scope>provided</scope>
</dependency>

【讨论】:

    【解决方案2】:

    解决方案是在依赖管理部分添加 slf4j 条目。

      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
          </dependency>
    
          <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
          </dependency>
          <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-reload4j</artifactId>
            <version>1.7.36</version>
          </dependency>
        </dependencies>
      </dependencyManagement>
    

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      相关资源
      最近更新 更多