本文介绍了错误:“由以下原因引起:java.lang.ClassNotFoundException:org.eclipse.paho.client.mqttv3.MqttCallback"当做java -jar myproyect.jar时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用java -jar运行proyect时遇到了该错误.我的代理是具有控制器类的MQTT服务器:

I got that error when I tried to run my proyect using java -jar. My proyect is a MQTT server that has a controller class:

public class ControladorMQTT

和一个回调类:

public class CallbackMQTT implements MqttCallback

似乎没有检测到回调分类.

and it seems that the callback clas is not beeing detected.

这是完整的错误:

C:\Users\Moreno\Documents\IntelliJ\ControladorMQTT\out\artifacts\ControladorMQTT_jar>java -jar ControladorMQTT.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/paho/client/mqttv3/MqttCallback
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.paho.client.mqttv3.MqttCallback
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

我有这样创建的MANIFEST.MF:

I have the MANIFEST.MF created like this:

Manifest-Version: 1.0
Main-Class: MQTT.ControladorMQTT
Class-Path: org.eclipse.paho.client.mqttv3.MqttCallback

编辑

我已将库C:\Users\Moreno\.m2\repository\org\eclipse\paho\org.eclipse.paho.client.mqttv3\1.2.0\org.eclipse.paho.client.mqttv3-1.2.0.jar添加到我的应用程序.jar directoy中,并修改了清单,使其看起来像这样:

I have added the library C:\Users\Moreno\.m2\repository\org\eclipse\paho\org.eclipse.paho.client.mqttv3\1.2.0\org.eclipse.paho.client.mqttv3-1.2.0.jar into my application .jar directoy and I have modified the MANIFEST to look like this:

Manifest-Version: 1.0
Main-Class: MQTT.ControladorMQTT
Class-Path: org.eclipse.paho.client.mqttv3-1.2.0.jar

但是它仍然不起作用.

推荐答案

您需要将外部依赖项(jar文件)包括到类路径中.像这样:

You need to include the external dependencies (jar files) into the classpath. Something like this:

java -cp C:\path\to\file.jar;C:\path\to\some-lib-jars\*;. -jar ControladorMQTT.jar

请参阅:

这篇关于错误:“由以下原因引起:java.lang.ClassNotFoundException:org.eclipse.paho.client.mqttv3.MqttCallback"当做java -jar myproyect.jar时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 13:01