首页 > 解决方案 > 在 Raspberry 上导入 java lib

问题描述

我试图在我的 Raspberry PI(https://github.com/scream3r/java-simple-serial-connector)上安装一个 java 串行通信库,所以我复制了 .so 文件(src/java/libs/linux/libjSSC -2.8_armhf.so) 到我的 lib 路径 (usr/lib/jvm/java-1.11.0-openjdk-armhf/lib)。

然后,当我测试 lib 代码示例时:

   import jssc.SerialPortList;

   public class Main {

       public static void main(String[] args) {
           String[] portNames = SerialPortList.getPortNames();
           for(int i = 0; i < portNames.length; i++){
           System.out.println(portNames[i]);
           }
       }
    }

我收到很多错误,说找不到 SerialPortList。

所以我想知道如何正确安装lib。错误:

 javac Main.java

 Main.java:1: error: package jssc does not exist
 import jssc.SerialPortList;
       ^
 ./SerialPortList.java:38: error: cannot find symbol
 private static SerialNativeInterface serialInterface;
               ^
 symbol:   class SerialNativeInterface
 location: class SerialPortList
 Main.java:6: error: cannot access SerialPortList
 String[] portNames = SerialPortList.getPortNames();
                     ^
 bad source file: ./SerialPortList.java
 file does not contain class SerialPortList
 Please remove or make sure it appears in the correct subdirectory of the 
 sourcepath.
./SerialPortList.java:43: error: cannot find symbol
    serialInterface = new SerialNativeInterface();
                          ^
 symbol:   class SerialNativeInterface
 location: class SerialPortList
 ./SerialPortList.java:44: error: cannot find symbol
    switch (SerialNativeInterface.getOsType()) {
            ^
 symbol:   variable SerialNativeInterface
 location: class SerialPortList
 ./SerialPortList.java:45: error: cannot find symbol
        case SerialNativeInterface.OS_LINUX: {
             ^
 symbol:   variable SerialNativeInterface
 location: class SerialPortList
 ./SerialPortList.java:50: error: cannot find symbol
        case SerialNativeInterface.OS_SOLARIS: {
             ^
 symbol:   variable SerialNativeInterface
 location: class SerialPortList
 ./SerialPortList.java:55: error: cannot find symbol
        case SerialNativeInterface.OS_MAC_OS_X: {
             ^
 symbol:   variable SerialNativeInterface
 location: class SerialPortList
 ./SerialPortList.java:60: error: cannot find symbol
        case SerialNativeInterface.OS_WINDOWS: {
             ^
  symbol:   variable SerialNativeInterface
  location: class SerialPortList
  ./SerialPortList.java:293: error: cannot find symbol
    if(SerialNativeInterface.getOsType() == 
  SerialNativeInterface.OS_WINDOWS){
       ^
  symbol:   variable SerialNativeInterface
  location: class SerialPortList
  ./SerialPortList.java:293: error: cannot find symbol
    if(SerialNativeInterface.getOsType() == 
   SerialNativeInterface.OS_WINDOWS){
                                            ^
   symbol:   variable SerialNativeInterface
   location: class SerialPortList
   ./SerialPortList.java:334: error: cannot find symbol
                    if(portHandle < 0 && portHandle != 
    SerialNativeInterface.ERR_PORT_BUSY){
                                                       ^
    symbol:   variable SerialNativeInterface
    location: class SerialPortList
    ./SerialPortList.java:337: error: cannot find symbol
                    else if(portHandle != 
     SerialNativeInterface.ERR_PORT_BUSY) {
                                          ^
     symbol:   variable SerialNativeInterface
     location: class SerialPortList
     13 errors

谢谢。

标签: javaraspberry-pi

解决方案


您需要将您的库添加为外部库。
检查如何在您的 IDE 上执行此操作:Intellij示例

编辑以匹配 OP 的 IDE:
Geany Tutorial for external library。

右键单击需要库的项目名称 -> 属性 -> 项目属性窗口打开。在类别树中选择“库”节点 -> 在项目属性窗口的右侧按下按钮“添加 JAR/文件夹” -> 选择您需要的 jars。


推荐阅读