4

When I try to do java -jar app.jar I get this:

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

My environment is: Java 1.8.0_92, Gradle 2.16, Eclipse Mars 4.5.0, Java FX. Also I have googled this error and I have tried everything from there , but with no success. I tried to add to the build.gradle as dependencies slfj-simple, log4j, logback-classic, slf4j-jdk14,junit,vaadin-slf4j-jdk14 but the error kept appearing.

When I run the application from Eclipse, everything works fine, the logs are written, only when I try to run the jar from command line, I get this ClassNotFoundException.

build.gradle looks like :

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.+"
}

Any help is greately appreciated.

Adriana Frunza
  • 53
  • 1
  • 1
  • 5
  • I appreciate the quick comeback :-) – GhostCat Sep 12 '17 at 17:23
  • For anyone else reading: Are you running your file.jar with same JDK version, as you compiled it ? (As a first step, I recommend to use same JDK, not even JRE of same version.) Just to exclude possible source of error. This fixed my problem with same symptoms :) Use this sample line to point to any JVM: "c:\Program Files\Java\jdk1.8.0_121\bin\java.exe" -jar d:\Path\To\Your\File.jar – Sold Out Oct 03 '17 at 08:49

3 Answers3

3

Simple: the classpath that you are using when trying to execute your JAR file does not contain the required sl4j logger library.

That is all there is to this. This is not about your build setup - it is about the setup that gets used when you run java on the command line!

See here for further reading.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I have tried to set the classpath from the command line and also I got the same error: – Adriana Frunza Aug 03 '17 at 08:56
  • 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 – Adriana Frunza Aug 03 '17 at 08:57
  • 1
    That is *AP*I jar. I wouldnt be surprised if slf4j also has some **implementation** jar. In other words: one jar might not do! – GhostCat Aug 03 '17 at 09:07
1

I have unzipped the zip file that was created in the dist folder and it works :) because in the lib folder there was the sl4j-api.

Adriana Frunza
  • 53
  • 1
  • 1
  • 5
0

It seems you need slf4j at runtime as well. Have a look at the gradle dependency configuration options

bipson
  • 64
  • 5