简体   繁体   English

NoClassDefFoundError: org/slf4j/LoggerFactory 在使用 gradle 创建可运行的 *.jar 时

[英]NoClassDefFoundError: org/slf4j/LoggerFactory while creating a runnable *.jar with gradle

I'm just trying to create a runnable *.jar file for my application with the help of gradle and the application plugin.我只是想在 gradle 和应用程序插件的帮助下为我的应用程序创建一个可运行的 *.jar 文件。

Building ends with no errors, manifest file is OK etc etc, but when it comes to running the *.jar file this happens:构建结束时没有错误,清单文件正常等等,但是在运行 *.jar 文件时会发生这种情况:

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构建.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.请注意,它确实包含 slf4j,但我仍然遇到异常。 Any ideas?有什么想法吗?

Thank you!谢谢!

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.是的,可能有更好的解决方案,但我在 1 个月内找不到它。

I think that the problem comes from conflicting definitions in imported libraries which also reference sl4j.我认为问题来自于也引用 sl4j 的导入库中的冲突定义。 Using maven, I had to add manually instruct to exclude these references from other libraries.使用 maven,我必须手动添加指令以从其他库中排除这些引用。

     <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
        <exclusions>
            <exclusion>
                <artifactId>org.slf4j</artifactId>
                <groupId>slf4j-api</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
        <exclusions>
            <exclusion>
                <artifactId>org.slf4j</artifactId>
                <groupId>slf4j-log4j12</groupId>
            </exclusion>
        </exclusions>
    </dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoClassDefFoundError: org/slf4j/LoggerFactory with logback - NoClassDefFoundError: org/slf4j/LoggerFactory with logback Gradle-线程“ main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Gradle - Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 线程“ main”中的Gradle异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Gradle Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 休眠-java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Hibernate - java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 问题 - java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory problem java.lang.NoClassDefFoundError: OSGi 中的 org/slf4j/LoggerFactory - java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory in OSGi Slf4j 和 Logback 错误:java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory - Slf4j and Logback error: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory Java + Maven +在线程“main”中生成可执行jar +异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Java + Maven + make executable jar + Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 如何在运行java jar应用程序时修复“java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory”错误 - how to fix “java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory” error when run java jar application RabbitMQ - 线程“main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - RabbitMQ - Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM