[英]ClassNotFoundException When running jar file but runs fine in Intellij
I created a small mqtt application using eclipse paho mqtt library in kotlin with Gradle in Intellij IDE.我使用 kotlin 中的eclipse paho mqtt 库和 Intellij Z58B5CC4FB56E11DC7520F716EC1763ABBZ 中的 Gradle 创建了一个小型 mqtt 应用程序。 it runs fine when running it through Intellij but when I build it and run the jar file that gets created I get a
NoClassDefFoundError
error.它在通过 Intellij 运行时运行良好,但是当我构建它并运行创建的 jar 文件时,我收到
NoClassDefFoundError
错误。
From other questions I have seen about this it looks like it has something to do with the class path but I am not sure what needs to be done if that is indeed the issue because I am using gradle and not jar files for libraries.从我看到的其他问题来看,它看起来与 class 路径有关,但我不确定如果这确实是问题需要做什么,因为我使用的是 gradle 而不是 Z68995FCBF432492D15484D04A9D2AC 文件。
I was following this tutorial我正在关注本教程
Here is my gradle file这是我的 gradle 文件
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.31'
id 'application'
}
group = 'me.package'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
}
}
dependencies {
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
test {
useJUnit()
}
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
}
compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
}
application {
mainClassName = 'com.publisher.MainKt'
}
tasks.jar {
manifest {
attributes 'Main-Class': 'com.publisher.MainKt'
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
And my MainKt file还有我的 MainKt 文件
package com.publisher
import org.eclipse.paho.client.mqttv3.*
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence
import java.io.File
fun main(args: Array<String>) {
val client = MqttClient("tcp://192.168.0.55:1883","publisher", MemoryPersistence())
val connOpts = MqttConnectOptions()
connOpts.isCleanSession = false
connOpts.isAutomaticReconnect = true
client.setCallback(object: MqttCallback {
override fun connectionLost(cause: Throwable?) {
println("Connection lost")
println(cause!!.message)
}
override fun messageArrived(topic: String?, message: MqttMessage?) {
println("Message Received for topic: $topic")
println("Message: ${message!!.payload}")
}
override fun deliveryComplete(token: IMqttDeliveryToken?) {
println("Message delivered")
}
})
try{
client.connect(connOpts)
println("Connected")
client.subscribe("config/+", 1) { topic, message ->
println("Getting configuration for $message")
val path = System.getProperty("user.dir")
val file = File("$path/${message}.json")
if(file.exists()){
client.publish("/devices/ + $message + /config", MqttMessage(file.readBytes()))
}
}
}catch (e: MqttException){
println("Error: ${e.localizedMessage}")
e.printStackTrace()
}
}
The way you start your application does not include the dependencies, meaning your MQTT driver and the Kotlin dependencies are not included.您启动应用程序的方式不包括依赖项,这意味着您的 MQTT 驱动程序和 Kotlin 依赖项不包括在内。
Do the following:请执行下列操作:
gradle distZip
# alternatively
gradle distTar
This will create a zip/tar file containing all the dependencies and a start script.这将创建一个包含所有依赖项和启动脚本的 zip/tar 文件。 Use that to start your application.
使用它来启动您的应用程序。
You could consider the Shadow plugin, as it is straightforward to use.您可以考虑使用Shadow插件,因为它使用起来很简单。 Your
build.gradle
would look something like this:您的
build.gradle
看起来像这样:
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.31'
// Shadow plugin
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'java'
}
group = 'me.package'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
}
}
dependencies {
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
test {
useJUnit()
}
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
}
compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
}
application {
mainClassName = 'com.publisher.MainKt'
}
tasks.jar {
manifest {
attributes 'Main-Class': 'com.publisher.MainKt'
}
}
So your fat JAR is generated in the /build/libs
directory with all the dependencies included.因此,您的胖 JAR 会在
/build/libs
目录中生成,其中包含所有依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.