2017-04-04 61 views
0

我已經建立了包含在pi4j庫中的項目jar文件。當我試圖發動對樹莓的jar文件LCD 1602和PI4J樹莓

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

我已手動導入pi4j的JAR的文件中的項目。感謝您的幫助

+0

需要包含在類路徑中pi4j罐子。編譯時類路徑並不意味着運行運行時會自動相同。它們不是像C – slipperyseal

+0

那樣靜態編譯還要確保你在pi上安裝了pi4j,因爲它安裝了pi4j罐子需要的本地庫 – slipperyseal

回答

0

SOlved,將lib文件夾(包含所有pi4j jar)添加到jar目錄。

+0

然後不要害怕將自己的答案標記爲「關閉」問題的解決方案;) –

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