java - 线程 "main"java.lang.NoClassDefFoundError : org/slf4j/LoggerFactory 中的 Gradle 异常

标签 java gradle slf4j intellij-idea-2016

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

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at com.sammy.CheckLog.(CheckLog.java:8) Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more

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

最佳答案

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

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

关于java - 线程 "main"java.lang.NoClassDefFoundError : org/slf4j/LoggerFactory 中的 Gradle 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42531127/

相关文章:

android - 如何过滤和执行特定的Gradle任务?

android - 无法合并dex-Android Studio 3.0.1

java - 使用 JMockit 模拟和验证 SLF4J

java - WebSphere App Server 中的 SLF4J 消息格式

java - retrofit 2.3.0 如何处理嵌套的json?

java - 在 1 个窗口中制作 2 个创建行 iDempiere

java - 如何在 Spring Boot Gradle 项目的 .jar 根目录中构建文件夹?

unit-testing - 类路径包含多个 SLF4J 与 Gradle 的绑定(bind)

java - 如何使其成为更简单的代码(Java,简单的字符串输入,说明它是否正确)

java - 如何在 ProcessBuilder 中运行长命令