Error: “Caused by: java.lang.ClassNotFoundException: org.eclipse.paho.client.mqttv3.MqttCallback” when doing java -jar myproyect.jar

心已入冬 提交于 2021-01-29 20:43:04

问题


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

and a callback class:

public class CallbackMQTT implements MqttCallback

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

This is the complete error:

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

I have the MANIFEST.MF created like this:

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

EDIT

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

but it still doesn't work.


回答1:


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

See:

  • java command documentation.



回答2:


Your Manifest Class-Path: declaration is wrong. See the documentation here

It should contain the list of Jars holding the libraries you have used in your application. You have added a single class org.eclipse.paho.client.mqttv3.MqttCallback, this will not work.

It should read something like this:

Manifest-Version: 1.0
Main-Class: MQTT.ControladorMQTT
Class-Path: paho-3.1.jar

And the paho-3.1.jar file should be in the same directory as your application jar.



来源:https://stackoverflow.com/questions/60563649/error-caused-by-java-lang-classnotfoundexception-org-eclipse-paho-client-mqt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!