首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >由java.lang.NoClassDefFoundError和ClassNotFoundException引起的错误

由java.lang.NoClassDefFoundError和ClassNotFoundException引起的错误
EN

Stack Overflow用户
提问于 2017-07-24 20:59:03
回答 1查看 917关注 0票数 1

我正在尝试实现一个简单的代码,从超声波读取数据,并使用加利福尼亚将其发送到服务器。问题是,当我使用debug时,它没有任何错误。但是,当我将代码导出到一个可运行的jar文件中并在我的raspberry pi上运行它时,它抛出了以下错误:

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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");
        }
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2017-07-24 21:02:25

该错误声明

代码语言:javascript
复制
Caused by: java.lang.ClassNotFoundException: org.eclipse.californium.elements.util.NamedThreadFactory

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45281302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档