【问题标题】:Gradle Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory线程“主”java.lang.NoClassDefFoundError 中的 Gradle 异常:org/slf4j/LoggerFactory
【发布时间】:2017-07-20 17:52:36
【问题描述】:

我已经尝试在我的项目中让它工作了三天,并创建了一个简单的项目来测试。我在这里搜索了非常相似的问题,但没有找到任何有助于解决我遇到的错误的方法。我什至重新格式化了我的 Fedora 25,因为它可以在 Windows 10 上运行,但仍然没有。我还启用了 Intellij-2016.3.4 注释处理中的设置和其他设置,根据其他答案应该已经修复它,但它不会改变任何东西。请任何帮助将不胜感激!!!

线程“主”java.lang.NoClassDefFoundError 中的异常: org/slf4j/LoggerFactory 在 com.sammy.CheckLog.(CheckLog.java:8) 原因: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory 在 java.net.URLClassLoader.findClass(URLClassLoader.java:381) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 更多

import lombok.extern.slf4j.Slf4j;

    @Slf4j
    public class CheckLog {

        public static void main(String... args){
            log.info("I'm Here!! ");
        }
    }

下面是我的 build.gradle 文件,其中定义了 lombok 以及相关的 slf4j 依赖项。

 /*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a commented-out sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/3.4/userguide/tutorial_java_projects.html
 */

// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'com.sammy.CheckLog'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compileOnly 'org.slf4j:slf4j-api:1.7.24'
    compileOnly 'org.slf4j:slf4j-simple:1.7.24'
    compileOnly 'org.projectlombok:lombok:1.16.14'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}

更新:更改为“编译”后,它在命令行上运行,而不是来自 Intellij,因为它似乎使用它自己的运行时来解释它,而 gradle 使用我定义的那个。 jdk1.8.0_121

【问题讨论】:

    标签: java gradle slf4j intellij-idea-2016


    【解决方案1】:

    问题是您没有在运行时添加 SL4J JAR 文件,因为您将它用于仅编译。 将 SLF4J 依赖项的 build.gradle 文件更改为 compile 而不是 compileOnly

    compile 'org.slf4j:slf4j-api:1.7.24'
    compile 'org.slf4j:slf4j-simple:1.7.24'
    

    【讨论】:

    • 谢谢!我现在可以在命令行上运行它以使其工作。但是,当我使用 Intellij 运行它时,它仍然会抛出相同的错误消息,并且因为我在 Intellij 中有一些数据库应用程序可以运行端到端测试,所以我也需要它在那里工作。
    • 尝试在 IntelliJ 中重新导入项目。
    • 它仍然失败:-(
    • 'at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)' 堆栈跟踪中缺少该行,这告诉我 Intellij 正在尝试使用它自己的运行时库执行代码而不是 gradle 用来运行程序的 jre 定义的代码。有办法改变吗?
    • 在我的机器上进行了测试:模拟了同样的问题,然后我转到“Gradle 项目”选项卡并单击“刷新所有 Gradle 项目”按钮,它通过更正库来解决问题进口的。 (我将我的文件作为 Gradle 项目导入,从我的项目文件夹中打开 build.gradle,没有 IntelliJ 配置)
    【解决方案2】:

    我有一些问题。 IntelliJ Idea 2016.3 和 Gradle 3.4.1

    初始化一个新项目:

    gradle init --type java-application

    在 build.gradle 中添加了几行

    compile 'org.slf4j:slf4j-api:1.7.24' compile 'org.slf4j:slf4j-simple:1.7.24'

    在 App.java 中添加了一行:

    ... static Logger logger = LoggerFactory.getLogger(App.class); ...

    现在您在 Idea 中有错误,但在终端项目中运行正常(使用 gradle 运行)

    Error in IntelliJ Idea

    更新:我发现如果我使用 gradle 3.3(而不是 3.4.1)初始化项目,则会出错。也许这种行为与这个问题有关:https://youtrack.jetbrains.com/issue/IDEA-167412

    【讨论】:

      【解决方案3】:

      更改:compileOnly 'org.slf4j:slf4j-api:1.7.24'compile 'org.slf4j:slf4j-api:1.7.24'

      【讨论】:

      • 谢谢!我现在可以在命令行上运行它以使其工作。但是,当我使用 Intellij 运行它时,它仍然会抛出相同的错误消息,并且因为我在 Intellij 中有一些数据库应用程序可以运行端到端测试,所以我也需要它在那里工作。
      • @Sammy65 它也应该在 Intellij 中工作。你能刷新你的项目吗?
      • 我已经刷新了几次项目但仍然没有乐趣
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多