java - java.lang.NoClassDefFoundError 和 ClassNotFoundException 引起的错误

标签 java raspberry-pi californium

我正在尝试实现一个简单的代码来从超声波读取数据并使用 Californium 将其发送到服务器。问题是,当我使用debug时,没有任何错误。然而,当我将代码导出到可运行的 jar 文件并在我的树莓派上运行它时,它会抛出以下错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: org/eclipse/californium/elements/util/NamedThreadFactory
at org.eclipse.californium.core.CoapServer.<init>(CoapServer.java:163)
at org.eclipse.californium.core.CoapServer.<init>(CoapServer.java:120)
at iot.main.device.DeviceClient.main(DeviceClient.java:34)
... 5 more
Caused by: java.lang.ClassNotFoundException: org.eclipse.californium.elements.util.NamedThreadFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 8 more

我不确定是什么原因造成的以及如何解决它,任何建议都会很有帮助。下面是代码:

package iot.main.device;

import static org.eclipse.californium.core.coap.CoAP.ResponseCode.BAD_REQUEST;
import static org.eclipse.californium.core.coap.CoAP.ResponseCode.CHANGED;

import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.server.resources.CoapExchange;

import com.pi4j.io.gpio.*;


public class DeviceClient {


private static GpioPinDigitalOutput sensorTriggerPin ;
private static GpioPinDigitalInput sensorEchoPin ;

final static GpioController gpio = GpioFactory.getInstance();

public static void main(String[] args) {


    double ultraVal;

    sensorTriggerPin =  gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04); // Trigger pin as OUTPUT
    sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05,PinPullResistance.PULL_DOWN); // Echo pin as INPUT

    CoapClient client = new CoapClient("coap://localhost/Ultrasonic");

    CoapServer server = new CoapServer();
    server.add(new UltraResource());
    server.start();

    while(true){
        try {
        Thread.sleep(2000);
        sensorTriggerPin.high(); // Make trigger pin HIGH
        Thread.sleep((long) 0.00001);// Delay for 10 microseconds
        sensorTriggerPin.low(); //Make trigger pin LOW

        while(sensorEchoPin.isLow()){ //Wait until the ECHO pin gets HIGH

        }
        long startTime= System.nanoTime(); // Store the surrent time to calculate ECHO pin HIGH time.
        while(sensorEchoPin.isHigh()){ //Wait until the ECHO pin gets LOW

        }
        long endTime= System.nanoTime(); // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.

        ultraVal = ((((endTime - startTime)/1e3)/2) / 29.1);
        System.out.println("Distance : " + ultraVal + " cm"); //Printing out the distance in cm  
        Thread.sleep(1000);

        CoapResponse response = client.put(Double.toString(ultraVal), MediaTypeRegistry.TEXT_PLAIN);

        if (response!=null) {

            System.out.println( response.getCode() );
            System.out.println( response.getOptions() );
            System.out.println( response.getResponseText() );

        } else {

            System.out.println("Request failed");

        }



        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

}

public static class UltraResource extends CoapResource {

    public String value = "Resource has not changed yet !!!";

    public UltraResource() {
        super("Ultrasonic");
        setObservable(true);
    }

    @Override
    public void handleGET(CoapExchange exchange) {
        exchange.respond(value);

    }


    @Override
    public void handlePUT(CoapExchange exchange) {
        String payload = exchange.getRequestText();
        System.out.println(payload + " cm");

        try {
            exchange.respond(CHANGED, payload);
            value = new String(payload);
            changed();
        } catch (Exception e) {
            e.printStackTrace();
            exchange.respond(BAD_REQUEST, "Invalid String");
        }
    }
}

}

最佳答案

错误指出

Caused by: java.lang.ClassNotFoundException: org.eclipse.californium.elements.util.NamedThreadFactory

这意味着您正在编写使用 Californium 依赖项的代码。这种依赖关系存在于您的开发人员内部。环境,但它不在您的 Raspberry Pi 的运行时环境内。

看看这个 other post这可能对你有帮助。另外这个 SO 网站可能会更好。

关于java - java.lang.NoClassDefFoundError 和 ClassNotFoundException 引起的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45281302/

相关文章:

java - 为什么 Eclipse 在使用 @See 引用它时导入一个类

raspberry-pi - 将 CAD 模型(Solidworks、AutoCAD 或 CATIA)与 Raspberry Pi 或 Arduino 传感器的实时测量连接起来

PHP 不保存到文件

java - 如何设置 Coap Observer 来监听响应

java - 如何删除java.io.IOException : Stream closed?

java - 如何提高java中正则表达式的性能

Java - 将else语句放入循环中

c++11 - 未解析的 std::__cxx11::basic_string::~basic_string 和 std::allocator<char>::~allocator() 符号

java - 如何处理自定义 Coap 选项(非标准)