线程中的Gradle异常“main” java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory [英] Gradle Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

查看:2511
本文介绍了线程中的Gradle异常“main” java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的项目中为此工作三天,并创建了一个简单的项目来测试。我在这里搜索了非常类似的问题,但没有发现任何有助于解决我遇到的错误的内容。我甚至会重新格式化Fedora 25,因为它在Windows 10上工作,但仍然没有任何结果。我也在Intellij-2016.3.4注释处理中的设置和其他设置中启用了根据其他答案应该修复它的方法,但它不会改变任何内容。


线程main中的异常java.lang.NoClassDefFoundError:
org / slf4j / LoggerFactory at
com.sammy.CheckLog。(CheckLog.java:8)导致:
java.lang.ClassNotFoundException:org.slf4j.LoggerFactory $ b $ java.net.URLClassLoader.findClass (URLClassLoader.java:381)
at com.intellij.rt.execution.application.AppMain.main(AppMain。)。 java:123)sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331) $ b $在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这里!! );




$ b $ p
$ b

下面是我的build.gradle文件,与相关的slf4j依赖关系。

  / * 
*此构建文件由Gradle的'init'任务生成。
*
*这个生成的文件包含一个注释掉的示例Java项目,以帮助您开始。
*有关更多详细信息,请参阅https://docs.gradle.org/3.4/userguide/tutorial_java_projects.html中的Gradle
*用户指南中的Java Quickstart一章。
* /

//应用java插件添加对Java
的支持apply插件:'java'
apply插件:'application'

mainClassName = 'com.sammy.CheckLog'

//在本节中,您将声明在哪里查找项目的依赖关系
repositories {
//使用'jcenter'解决您的依赖关系。
//你可以在这里声明任何Maven / Ivy /文件库。
jcenter()
}

//在本节中,您将声明生产和测试代码的依赖项
dependencies {
//生产代码在编译时使用SLF4J日志API
compileOnly'org.slf4j:slf4j-api:1.7.24'
compileOnly'org.slf4j:slf4j-simple:1.7.24'
compileOnly' org.projectlombok:lombok:1.16.14'

//声明你想在你的测试中使用的你最喜欢的测试框架的依赖关系。
// Gradle Test任务也支持TestNG。只需将
// testCompile依赖项更改为testCompile'org.testng:testng:6.8.1'并将
//'test.useTestNG()'添加到您的构建脚本中。
testCompile'junit:junit:4.12'
}

UPDATE :更改为compile后,它在命令行上运行,而不是从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'


I have tried to get this working for three days now in my project and created a simple project to test. I searched around very similar issues on here but didn't find anything that helped with how to solve the error I'm getting. I went even as far as reformatting my Fedora 25 as it works on a Windows 10 but still nothing. I have also enabled in both settings and other settings in Intellij-2016.3.4 annotation processing which according to other answers should have fixed it but, it doesn't change anything. Please any help will be appreciated!!!

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!! ");
        }
    }

Below is my build.gradle file having lombok defined along with the associated slf4j dependencies.

 /*
 * 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'
}

UPDATE: After changing to "compile", this runs on the commandline not from Intellij as it seems to use it's own runtime to interpret it while gradle uses the one I've defined. jdk1.8.0_121

解决方案

The problem is that you have not added the SL4J JAR files at runtime, because you used it for compile-only. Change your build.gradle file for the SLF4J dependencies to compile instead of compileOnly:

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

这篇关于线程中的Gradle异常“main” java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆