问题
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
回答1:
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'
回答2:
I have some problem. IntelliJ Idea 2016.3 and Gradle 3.4.1
Initialized a new project with:
gradle init --type java-application
added couple lines to build.gradle
compile 'org.slf4j:slf4j-api:1.7.24'
compile 'org.slf4j:slf4j-simple:1.7.24'
added line to App.java:
...
static Logger logger = LoggerFactory.getLogger(App.class);
...
And now you have error in Idea, but in terminal project will run ok (with gradle run)
Error in IntelliJ Idea
Update: I found that if I'm initializing a project with a gradle 3.3 (not 3.4.1) then error leave. Maybe this behavior related to this issue: https://youtrack.jetbrains.com/issue/IDEA-167412
回答3:
Change: compileOnly 'org.slf4j:slf4j-api:1.7.24'
to compile 'org.slf4j:slf4j-api:1.7.24'
来源:https://stackoverflow.com/questions/42531127/gradle-exception-in-thread-main-java-lang-noclassdeffounderror-org-slf4j-logg