2017-08-03 371 views
1

當我嘗試做Java的罐子app.jar我得到這個:錯誤拋出java.lang.ClassNotFoundException:org.slf4j.LoggerFactory試圖執行的jar

Exception in thread "JavaFX Application Thread" Exception in thread "main" 
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 
    at tao.qaflash.GUI.<clinit>(GUI.java:14) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:348) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$156(LauncherImpl.java:352) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    at java.lang.Thread.run(Thread.java:745) 
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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 12 more 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.NullPointerException 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
    ... 5 more 

我的環境是:Java的1.8。 0_92,Gradle 2.16,Eclipse Mars 4.5.0,Java FX。 另外我GOOGLE了這個錯誤,我嘗試了一切從那裏,但沒有成功。我試圖添加到的build.gradle作爲依賴slfj-簡單,log4j的,的logback經典,SLF4J-JDK14,JUnit的不同,Vaadin-SLF4J-JDK14但錯誤連續出現。

當我從Eclipse運行應用程序,一切工作正常,將記錄寫入,只有當我嘗試運行命令行的罐子,我得到這個ClassNotFoundException異常。

的build.gradle樣子:

dependencies { 
    compile 'net.sf.staf:jstaf:3.4.4' 

    // SLF4J API module which will be used for logging 
    compile 'org.slf4j:slf4j-api:1.7.21' 

    // Log4J2 logging framework including the binding module to slf4j 
    compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.5' 
    compile 'org.apache.logging.log4j:log4j-api:2.5' 
    compile 'org.apache.logging.log4j:log4j-core:2.5' 

    testCompile 'junit:junit:4.12' 
    testCompile "org.testfx:testfx-core:4.0.+" 
    testCompile "org.testfx:testfx-junit:4.0.+" 
} 

任何幫助greately讚賞。

+0

我欣賞的快速復出:-) – GhostCat

+0

爲別人讀:你運行與同JDK版本的file.jar,你編譯它? (作爲第一步,我建議使用相同的JDK,甚至不要使用相同版本的JRE。)只是爲了排除可能的錯誤來源。這固定我的問題與相同的症狀:) 使用此示例行指向任何JVM: 「c:\ Program Files \ Java \ jdk1.8.0_121 \ bin \ java.exe」 - jar d:\ Path \ To \ Your \ File.jar –

回答

1

很簡單:類路徑試圖當您使用執行您的JAR文件不包含所需sl4j記錄庫。

這是所有有就是爲了這個。這是你構建設置 - 這是關於設置當您運行在命令行上java是被使用!

進一步的閱讀可見here

+0

我試圖從命令行設置類路徑,並且我得到了同樣的錯誤: –

+0

java -jar app.jar -classpath C:\ Users \ Administrator \ .gradle \ caches \ modules-2 \ files-2.1 \ org.slf4j \ slf4j-api \ 1.7.21 \ 139535a69a4239db087de9bab0bee568bf8e0b70 \ slf4j-api-1.7.21.jar –

+0

即* AP * I jar。如果slf4j也有一些**實現** jar,我不會感到驚訝。換句話說:一個罐子可能不行! – GhostCat

1

我已經解壓縮在dist文件夾中創建的zip文件,它的工作原理:)因爲在lib文件夾中有sl4j-api。