2017-03-01 63 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

相關問題