2017-03-01 68 views
0

我试图在我的项目中得到这个工作三天,并创建了一个简单的项目来测试。我在这里搜索了非常类似的问题,但没有发现任何有助于解决我遇到的错误的内容。我甚至会重新格式化Fedora 25,因为它在Windows 10上工作,但仍然没有任何结果。我也在Intellij-2016.3.4注释处理中的设置和其他设置中启用了根据其他答案应该修复它的方法,但它不会改变任何内容。请任何帮助将不胜感激!线程“main”中的Gradle异常java.lang.NoClassDefFoundError:org/slf4j/LoggerFactory

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

下面是我与关联SLF4J依赖沿定义为龙目岛的build.gradle文件。

/* 
* 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:更改为“编译”后,这个,因为它似乎使用它自己的运行时的gradle同时使用我已经定义了一个解释它运行在不从的IntelliJ的命令行。 jdk1.8.0_121

回答

1

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

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

谢谢!我可以在命令行上运行它,现在就可以运行它。但是,当我使用Intellij运行它时,它仍会抛出相同的错误消息,并且因为Intellij中有一些数据库应用程序用于运行端到端测试,所以我需要它在其中工作。 – Sammy65

+0

尝试在IntelliJ中重新导入项目。 – Boschi

+0

它仍然失败:-( – Sammy65

1

变化:compileOnly 'org.slf4j:slf4j-api:1.7.24'compile 'org.slf4j:slf4j-api:1.7.24'

+0

谢谢!我可以在命令行上运行它,现在就可以运行它。但是,当我使用Intellij运行它时,它仍会抛出相同的错误消息,并且因为Intellij中有一些数据库应用程序用于运行端到端测试,所以我需要它在其中工作。 – Sammy65

+0

@ Sammy65它也应该在Intellij工作。你能刷新你的项目吗? – Jens

+0

我已经刷新了这个项目好几次了,但还是没有快乐 – Sammy65

2

我有一些问题。 IntelliJ IDEA的2016.3和摇篮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); ...

现在你有想法的错误,但在终端项目将运行正常(与gradle这个运行)

Error in IntelliJ Idea

更新:我发现,如果我初始化了一个项目,一个gradle这个3.3(不3.4.1)然后错误离开。也许这种行为与此问题有关:https://youtrack.jetbrains.com/issue/IDEA-167412

相关问题