尝试执行jar时出错java.lang.ClassNotFoundException:org.slf4j.LoggerFactory

时间:2017-08-03 08:30:12

标签: java logging slf4j slf4j-api

当我尝试做java -jar app.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。 此外,我已经搜索了这个错误,我已经尝试了一切,但没有成功。我尝试将build.gradle添加为依赖项slfj-simple,log4j,logback-classic,slf4j-jdk14,junit,vaadin-slf4j-jdk14但错误仍然出现。

当我从Eclipse运行应用程序时,一切正常,日志被写入,只有当我尝试从命令行运行jar时,才会得到这个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.+"
}

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

简单:尝试执行您的JAR文件时使用的类路径 包含所需的sl4j记录器库。

这就是全部。这是关于您的构建设置 - 它是关于在命令行上运行java时使用的 setup

有关详细信息,请参阅here

答案 1 :(得分:1)

我解压缩了在dist文件夹中创建的zip文件并且它正常工作:)因为在lib文件夹中有sl4j-api。

答案 2 :(得分:0)

似乎你在运行时也需要slf4j。看看gradle dependency configuration options