java - pi4j 无法使用示例代码进行初始化

标签 java exception raspberry-pi

我直接使用示例代码 from the webpage我仍然收到此错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.pi4j.io.gpio.RaspiGpioProvider.<init>(RaspiGpioProvider.java:47)
    at com.pi4j.io.gpio.GpioFactory.getDefaultProvider(GpioFactory.java:102)
    at com.pi4j.io.gpio.impl.GpioControllerImpl.<init>(GpioControllerImpl.java:67)
    at com.pi4j.io.gpio.GpioFactory.getInstance(GpioFactory.java:85)
    at test.main(test.java:21)
Caused by: java.lang.NullPointerException
    at com.pi4j.util.NativeLibraryLoader.load(NativeLibraryLoader.java:218)
    at com.pi4j.wiringpi.Gpio.<clinit>(Gpio.java:138)
    ... 5 more

这是我使用的页面中的代码,我更改的只是要测试的类名称。

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

/**
 * This example code demonstrates how to setup a listener
 * for GPIO pin state changes on the Raspberry Pi.  
 * 
 * @author Robert Savage
 */
public class test {

    public static void main(String args[]) throws InterruptedException {
        System.out.println("<--Pi4J--> GPIO Listen Example ... started.");

        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        // provision gpio pin #02 as an input pin with its internal pull down resistor enabled
        final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN);

        // create and register gpio pin listener
        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                // display pin state on console
                System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
            }

        });

        System.out.println(" ... complete the GPIO #02 circuit and see the listener feedback here in the console.");

        // keep program running until user aborts (CTRL-C)
        for (;;) {
            Thread.sleep(500);
        }

        // stop all GPIO activity/threads by shutting down the GPIO controller
        // (this method will forcefully shutdown all GPIO monitoring threads and scheduled tasks)
        // gpio.shutdown();   <--- implement this method call if you wish to terminate the Pi4J GPIO controller        
    }
}

我正在运行树莓派

最佳答案

我看起来您还没有安装 native 库(WiringPi)。 可以肯定的是,您是否遵循了此处的所有内容:http://pi4j.com/install.html

关于java - pi4j 无法使用示例代码进行初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27747195/

相关文章:

java - 为什么类文件不包含足够的信息来提供 NPE 中为空的指针的名称?

exception - RISC-V SYSTEM 指令如何实现为陷阱?

android-studio - Android studio : I am getting the following error "java.util.NoSuchElementException". 但是,我无法追踪错误

c++ - 如何阻塞直到 WiringPi 完成发送串行数据(UART)?

java - 设计 RESTful 登录服务

java - RestTemplate.postForObject - 错误 : org. springframework.web.client.HttpClientErrorException:400 错误请求

java - 使用弹出窗口将项目添加到数组列表

java - 为什么未登录异常堆栈跟踪?

linux - 在 supervisorctl ERROR 上出现 Supervison 错误(没有这样的过程)

我不明白的Python代码错误消息