【问题标题】:Gradle: Adding Classes to the JAR File's ClasspathGradle:将类添加到 JAR 文件的类路径
【发布时间】:2017-08-10 21:10:47
【问题描述】:

我希望标题本身是描述性的,但为了清楚起见,我试图在 greeting-service 中包含 jar(即 error-handling-service.jar)之一。罐子。构建后,我在新项目(即TestApplication)中包含了 greeting-service.jar 但在执行 TestApplication 我得到了(顺便说一句,TestApplication 不是 gradle 项目)

Exception in thread "main" java.lang.NoClassDefFoundError: co/common/exception/BaseException
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

co.common.exception.BaseException 是 error-handling-service 模块中的一个类

根据question 这里。我包括了

manifest {
            attributes(
                    "Manifest-Version": "1.0",
                    "Class-Path": configurations.compile.collect { it.getName() }.join(' ')
            )
        }

这里是 greeting-service 的 build.gradle,它依赖于错误处理服务

buildscript {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile('org.apache.commons:commons-lang3:3.0')
    compile('org.apache.commons:commons-collections4:4.0')
    compile('org.slf4j:slf4j-api:1.7.25')
    compile('co.common:error-handling-service:1.0.0-SNAPSHOT')
    testCompile("org.mockito:mockito-all:1.9.5")
    testCompile('junit:junit:4.12')
}

jar {
    baseName = 'greeting-service'
    version = '1.0.0-SNAPSHOT'
    manifest {
        attributes(
                "Manifest-Version": "1.0",
                "Class-Path": configurations.compile.collect { it.getName() }.join(' ')
        )
    }
}

group = 'co.common'
version = '1.0.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8


task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

在成功构建 greeting-service 后,我在 TestApplication 中包含了 greeting-service.jar,但我仍然收到上述相同的异常。

Manifest-Version: 1.0
Class-Path: commons-lang3-3.0.jar commons-io-2.4.jar commons-collections
 4-4.0.jar slf4j-api-1.7.25.jar error-handling-service-1.0.0-SNAPSHOT.
 jar commons-logging-1.1.3.jar

为什么会发生这种情况,应该怎么做?

【问题讨论】:

    标签: java gradle jar build.gradle


    【解决方案1】:

    NoClassDefFoundError 在 Java 中出现时,Java 虚拟机无法在运行时找到在编译时可用的特定类。例如,如果我们有一个类的方法调用或访问一个类的任何静态成员,并且该类在运行时不可用,那么 JVM 将抛出 NoClassDefFoundError。

    因此,假设您的 gradle 构建成功通过 - 我可能会断定您提供的 error-handling-service-1.0.0-SNAPSHOT.jar 错误,它不包含 co/common/exception/BaseException 类。

    你应该仔细检查这个罐子的实际内容。

    【讨论】:

    • jar的内容不包含error-handling-service-1.0.0-SNAPSHOT.jar的类。但这就是我想知道我的配置看起来不错还是我遗漏了什么?
    • 您的构建文件看起来不错。但错误是由于缺少类co/common/exception/BaseException 引起的。因此,您应该将带有此类的 .jar 文件包含到构建路径中。或者,您可以从代码中删除此类的使用。您可以使用gradle dependencies 命令检查您的 gradle 项目的实际依赖关系
    【解决方案2】:

    不确定这是否有帮助,但如果您的 error-handling-service.jar 已经打包并准备好作为依赖项包含到您的 greeting-service.jar 中,那么您应该将其设为依赖项。

    例如:

    1. 在您的项目中,创建一个名为“lib”的文件夹
    2. 在“lib”文件夹中添加您的 error-handling-service.jar
    3. 将以下内容添加到您的依赖项块中:

      依赖{ 编译 fileTree(dir: 'lib', include: ['*.jar']) ... }

    基本上,您放入“lib”文件夹的每个 *.jar 都会在您运行构建时添加到您的应用程序 jar 中。

    【讨论】:

    • 我想避免greeting-service.jar的用户再次包含greeting-service的依赖。我的罐子可以即插即用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2021-04-22
    • 2018-11-07
    • 2015-09-27
    • 2015-03-02
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多