You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle打包Jar后在其他项目中出现ClassNotFoundException问题求助

问题描述

使用Gradle作为构建工具、IntelliJ IDE作为开发环境,在build.gradle中引入两个本地Jar作为依赖(取自本地目录),自定义了生成项目Jar的任务,但生成的Jar在其他项目中使用时出现类找不到异常。

项目的build.gradle内容

plugins {
    id 'java'
}

group 'com.vodafone.tv'
version '1.0'

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
    flatDir {
//these are the directories from where I am picking up the local jars
        dirs "C:\\Users\\debanjan.bhatt\\Desktop\\vtv-plugin-base-master"
        dirs "C:\\Users\\debanjan.bhatt\\Desktop"
    }
}
//task to generate jar
task buildPlugin(type: Jar) {
    manifest {
        attributes 'Manifest-Version': '1.0',
                'Plugin-Id': 'vtv-mqtt',
                'Plugin-Version': '1.0.0'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

dependencies {
    implementation(':vtv-plugin-base') // this is my first local jar 1
    implementation(name: 'org.eclipse.paho.client.mqttv3-1.2.6')   //this is my seconds local jar 2
    implementation group: 'com.consol.citrus', name: 'citrus-java-dsl', version: '2.8.0'
    implementation group: 'org.json', name: 'json', version: '20201115'
    compileOnly(group: 'org.pf4j', name: 'pf4j', version: '3.6.0')
    annotationProcessor(group: 'org.pf4j', name: 'pf4j', version: "3.6.0")
    implementation group: 'org.testng', name: 'testng', version: '7.4.0'
    implementation group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.30'
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}

test {
    useJUnitPlatform()
}

错误信息

09:43:50,216 [ INFO  ] java.lang.ClassNotFoundException: org.eclipse.paho.client.mqttv3.MqttClientPersistence
解决方法

问题根源在于自定义Jar任务未正确包含所有运行时依赖类,且使用了Gradle已废弃的compile配置,导致本地依赖的Paho MQTT客户端类未被打包进最终Jar。

1. 修改build.gradle的Jar任务和依赖配置

替换原自定义任务,改用runtimeClasspath获取所有运行时依赖,并确保包含项目自身编译类:

plugins {
    id 'java'
}

group 'com.vodafone.tv'
version '1.0'

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
    flatDir {
        // 合并目录声明,简化写法
        dirs "C:\\Users\\debanjan.bhatt\\Desktop\\vtv-plugin-base-master", "C:\\Users\\debanjan.bhatt\\Desktop"
    }
}

// 修正后的Jar生成任务
task buildPlugin(type: Jar) {
    manifest {
        attributes 'Manifest-Version': '1.0',
                'Plugin-Id': 'vtv-mqtt',
                'Plugin-Version': '1.0.0'
    }
    // 使用runtimeClasspath替代废弃的compile配置,包含所有运行时依赖
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    // 确保包含当前项目的编译类
    from sourceSets.main.output
}

dependencies {
    implementation ':vtv-plugin-base'
    // 简化本地Jar依赖声明,与第一个保持一致格式
    implementation ':org.eclipse.paho.client.mqttv3-1.2.6'
    implementation group: 'com.consol.citrus', name: 'citrus-java-dsl', version: '2.8.0'
    implementation group: 'org.json', name: 'json', version: '20201115'
    compileOnly group: 'org.pf4j', name: 'pf4j', version: '3.6.0'
    annotationProcessor group: 'org.pf4j', name: 'pf4j', version: "3.6.0"
    implementation group: 'org.testng', name: 'testng', version: '7.4.0'
    implementation group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.30'
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}

test {
    useJUnitPlatform()
}

2. 关键修改说明

  • 替换configurations.compile为configurations.runtimeClasspath:Gradle 7及以上版本中compile配置已被废弃,runtimeClasspath包含所有运行时必需的依赖,确保本地Jar的类被打包。
  • 添加from sourceSets.main.output:避免只打包依赖类而遗漏项目自身的编译代码。
  • 简化本地Jar声明:第二个本地Jar用:org.eclipse.paho.client.mqttv3-1.2.6格式,确保Gradle正确识别本地依赖。

3. 重新生成Jar并验证

执行以下命令生成Jar:

# Windows系统
gradlew.bat buildPlugin

# Linux/macOS系统
./gradlew buildPlugin

生成的Jar会包含所有依赖类,在其他项目中使用时即可解决ClassNotFoundException问题。

内容的提问来源于stack exchange,提问作者Harish Algat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 13:05:01