简体   繁体   English

ClassNotFoundException 运行 jar 文件但在 Intellij 中运行良好

[英]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.

相关问题 在命令行上运行时出现ClassNotFoundException,但在IntelliJ IDEA上运行良好 - ClassNotFoundException when running on command line but runs fine on IntelliJ IDEA JavaFX应用程序在IntelliJ中运行Main时运行良好,内置jar无法运行 - JavaFX application runs fine when running Main in IntelliJ, built jar doesn't run 我的程序在Eclipse中运行良好,但抛出作为jar文件运行的异常 - My program runs fine in Eclipse but throws exception running as a jar file Hadoop MapReduce程序在Eclipse中运行良好,但在导出到.jar文件时却无法正常运行 - Hadoop MapReduce program runs fine in Eclipse but not when exported to .jar file 程序在Eclipse中可以正常运行,但是在导出到.jar文件时却不能正常运行 - program runs fine in Eclipse but not when exported to .jar file 运行可执行jar时ClassNotFoundException - ClassNotFoundException when running executable jar 运行hadoop jar时发生ClassNotFoundException - ClassNotFoundException when running hadoop jar 运行 JAR 时出现 ClassNotFoundException,在 IntelliJ IDEA 中运行时没有错误 - ClassNotFoundException upon running JAR, no errors while running in IntelliJ IDEA 代码在IDE中可以正常运行,但不能作为Jar文件运行 - Code runs fine in IDE but not run as Jar file 程序可以在IDE中正常运行,但不能作为Jar文件运行 - Program runs fine in IDE but not as Jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM