LCD 1602和PI4J覆盆子

时间:2017-04-04 07:26:42

标签: java raspberry-pi lcd

我已经建立了项目的jar文件,并包含在pi4j的库中。当我尝试在树莓上启动jar文件时

 sudo java -jar LCD1602.jar

我有这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory
        at lcd1602.LCD1602.main(LCD1602.java:19)
Caused by: java.lang.ClassNotFoundException: com.pi4j.io.gpio.GpioFactory
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

源码:

package lcd1602;

import com.pi4j.component.lcd.LCDTextAlignment;
import com.pi4j.component.lcd.impl.GpioLcdDisplay;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.system.NetworkInfo;

public class LCD1602 {
    public final static int LCD_ROW_1 = 0;
    public final static int LCD_ROW_2 = 1;

    public static void main(String[] args) throws Exception {

        System.out.println("16X2 LCD Example with Raspberry Pi using Pi4J and JAVA");

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

        // initialize LCD
        final GpioLcdDisplay lcd = new GpioLcdDisplay(2,    // number of row supported by LCD
                                                16,       // number of columns supported by LCD
                                                RaspiPin.GPIO_23,  // LCD RS pin
                                                RaspiPin.GPIO_24,  // LCD strobe pin
                                                RaspiPin.GPIO_04,  // LCD data bit D4
                                                RaspiPin.GPIO_17,  // LCD data bit D5
                                                RaspiPin.GPIO_27,  // LCD data bit D6
                                                RaspiPin.GPIO_22); // LCD data bit D7

        lcd.clear();
        Thread.sleep(1000);

        lcd.write(LCD_ROW_1, "WeArGenius");
        lcd.write(LCD_ROW_2, " ???");

        Thread.sleep(2000);
        for (String ipAddress : NetworkInfo.getIPAddresses()){
            System.out.println("IP Addresses      :  " + ipAddress);
            lcd.writeln(LCD_ROW_2,ipAddress,LCDTextAlignment.ALIGN_CENTER);
        }

        gpio.shutdown();
    }
}

我已经在项目中手动导入jar的pi4j文件。谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

SOlved,将lib文件夹(包含所有jar的pi4j)添加到jar目录。

答案 1 :(得分:0)

package i2clcddisplaytest;

import com.pi4j.component.lcd.impl.I2CLcdDisplay;
import com.pi4j.component.lcd.*;

public class I2cLcdDisplayTest {

    public static void main(String[] args) throws Exception {
        I2CLcdDisplay lcd = new I2CLcdDisplay(
                2,  //     * @param rows
                16, //     * @param columns
                1,  //     * @param i2cBus
                39, //     * @param i2cAddress
                3,  //     * @param backlightBit
                0,  //     * @param rsBit
                1,  //     * @param rwBit
                2,  //     * @param eBit
                7,  //     * @param d7
                6,  //     * @param d6
                5,  //     * @param d5
                4   //     * @param d4
        );
        lcd.write(1, "test", LCDTextAlignment.ALIGN_CENTER);
    }
}

此代码适用于LCM1602 IIC V1