【发布时间】:2021-06-12 05:26:22
【问题描述】:
我使用 Kotlin 中的 eclipse paho mqtt library 和 Intellij IDE 中的 Gradle 创建了一个小型 mqtt 应用程序。通过 Intellij 运行它时它运行良好,但是当我构建它并运行创建的 jar 文件时,我收到 NoClassDefFoundError 错误。
从我看到的其他问题来看,它看起来与类路径有关,但我不确定如果这确实是问题需要做什么,因为我使用的是 gradle 而不是库的 jar 文件.
我关注了这个tutorial
这是我的 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)
}
}
}
还有我的 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()
}
}
【问题讨论】:
-
你应该试试 gradle 的应用插件,构建一个包含所需库的应用程序包。
-
@dpr 在我正在关注的教程中(我在上面链接了它,因为我忘了提及它)并没有说什么。我确实快速浏览了插件的文档,但我没有立即看到任何关于包含其他依赖项/库的信息
-
jar文件的内容是什么?您可以使用任何 zip 工具(如 7zip)打开它或在命令行上解压缩。你是如何开始你的申请的?使用 java -jar
? -
@JensBaitinger jar 文件包含
MainKt$main$1.class、MainKt$main$2.class和MainKt.class文件以及带有清单的META_INF文件夹和.kotlin_module文件。目前我只是通过右键单击 intellij 中的文件并选择运行来运行 jar 文件,但是在执行java -jar <jar-file-name>.jar时会发生同样的事情 -
是 MainKt 在正确的文件夹中(根据它的包)还是在根目录中?
标签: java kotlin gradle intellij-idea jar