NoClassDefFoundError: org/slf4j/LoggerFactory whil

2019-09-06 02:47发布

问题:

I'm just trying to create a runnable *.jar file for my application with the help of gradle and the application plugin.

Building ends with no errors, manifest file is OK etc etc, but when it comes to running the *.jar file this happens:

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at main.Launcher.<clinit>(Launcher.java:19)
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)
    ... 1 more
Exception in thread "main" 
Process finished with exit code 1

It seems obvious that I am missing the library, but it is specified:

build.gradle

group 'Comparator'
version '0.9'

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "main.Launcher"

compileJava {
    //enable compilation in a separate daemon process
    options.fork = true
    //enable incremental compilation
    options.incremental = true
}

repositories {
    mavenCentral()

}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    // GraphStream Core
    compile group: 'org.graphstream', name: 'gs-core', version: '1.3'
    // GraphStream
    //UI
    compile group: 'org.graphstream', name: 'gs-ui', version: '1.3'
    // GraphStream
    //algo
    compile group: 'org.graphstream', name: 'gs-algo', version: '1.3'
    // Jena
    // https://mvnrepository.com/artifact/org.apache.jena/jena-arq
    compile group: 'org.apache.jena', name: 'jena-arq', version: '2.13.0'
    compile group: 'org.apache.jena', name: 'jena-querybuilder', version: '2.13.0'

    // Log
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'

    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.0.9'
    compile group: 'ch.qos.logback', name: 'logback-core', version: '1.0.9'
    // https://mvnrepository.com/artifact/org.jgrapht/jgrapht-core
    compile group: 'org.jgrapht', name: 'jgrapht-core', version: '0.9.2'
    compile group: 'org.jgrapht', name: 'jgrapht-jdk1.5', version: '0.7.3'
    compile group: 'org.jgrapht', name: 'jgrapht-ext', version: '0.9.2'
    // CSV parser
    compile group: 'com.univocity', name: 'univocity-parsers', version: '1.0.0'

    // https://mvnrepository.com/artifact/com.google.guava/guava
    compile group: 'com.google.guava', name: 'guava', version: '11.0.2'


}

jar {
    manifest {
        attributes 'Main-Class': mainClassName,
                'Class-Path': configurations.runtime.files.collect { "$it.name" }.join(' ')
    }
}

The created manifest is as follows:

Manifest-Version: 1.0
Main-Class: main.Launcher
Class-Path: gs-core-1.3.jar gs-ui-1.3.jar gs-algo-1.3.jar jena-arq-2.1
 3.0.jar jena-querybuilder-2.13.0.jar slf4j-api-1.7.21.jar logback-cla
 ssic-1.0.9.jar logback-core-1.0.9.jar jgrapht-core-0.9.2.jar jgrapht-
 jdk1.5-0.7.3.jar jgrapht-ext-0.9.2.jar univocity-parsers-1.0.0.jar gu
 ava-11.0.2.jar junit-4.12.jar pherd-1.0.jar mbox2-1.0.jar scala-libra
 ry-2.10.1.jar commons-math-2.1.jar commons-math3-3.4.1.jar jfreechart
 -1.0.14.jar jena-core-2.13.0.jar httpclient-4.2.6.jar jsonld-java-0.5
 .1.jar httpclient-cache-4.2.6.jar libthrift-0.9.2.jar commons-csv-1.0
 .jar commons-lang3-3.3.2.jar slf4j-log4j12-1.7.6.jar log4j-1.2.17.jar
  apache-jena-libs-2.13.0.pom jgraphx-2.0.0.1.jar jgraph-5.13.0.0.jar 
 jsr305-1.3.9.jar hamcrest-core-1.3.jar jcommon-1.0.17.jar itext-2.1.5
 .jar jena-iri-1.1.2.jar xercesImpl-2.11.0.jar httpcore-4.2.5.jar comm
 ons-codec-1.6.jar jackson-core-2.3.3.jar jackson-databind-2.3.3.jar j
 ena-tdb-1.1.2.jar bcmail-jdk14-138.jar bcprov-jdk14-138.jar jackson-a
 nnotations-2.3.0.jar jcl-over-slf4j-1.7.7.jar commons-logging-1.1.1.j
 ar xml-apis-1.4.01.jar

Note that it does include slf4j, but i am still getting the exception. Any ideas?

Thank you!

回答1:

After some time i've come to the (what i think is) solution. It was a big problem with different dependencies on the generated classpath. Generating the project from scratch and importing code and resources proved a succesfull build.

If you encounter the same error i propose the same solution, just rebuild the project from scratch.

Yes, there probably is a better solution but i could not find it in 1 month.