简体   繁体   中英

Error caused by java.lang.NoClassDefFoundError and ClassNotFoundException

I am trying to implement a simple code to read the data from the ultrasonic and send it to a server by using Californium. The problem is that when I use debug, it doesn't have any error. However when I export the code to a runable jar file and run it on my raspberry pi, it throws the following errors:

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

I am not sure what caused this and how to solve it, any suggestion would be much helpful. Below is the code:

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");
        }
    }
}

}

The error states that

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

This means you are writing code that uses a dependency from Californium. That dependency is within your dev. environment but it is not within the runtime environment of your Raspberry Pi.

Have a look at this other post which may help you. Also this SO site may be better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM