簡體   English   中英

由java.lang.NoClassDefFoundError和ClassNotFoundException引起的錯誤

[英]Error caused by java.lang.NoClassDefFoundError and ClassNotFoundException

我正在嘗試實現一個簡單的代碼,以從超聲波讀取數據並將其通過Californium發送到服務器。 問題是,當我使用調試時,它沒有任何錯誤。 但是,當我將代碼導出到可運行的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的運行時環境內。

看看這可能對您有幫助的其他帖子 同樣,該SO網站可能會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM