【问题标题】:Need Help to get librxtxSerial.jnilib into runnable jar file需要帮助将 librxtxSerial.jnilib 放入可运行的 jar 文件
【发布时间】:2016-10-26 14:15:04
【问题描述】:
public static void main(String[] args) { 

portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
             if (portId.getName().equals("/dev/cu.usbserial-FTCC026H")) {
                 System.out.println("Connecting.....");
        //                if (portId.getName().equals("/dev/term/a")) {
                 ReadReadings reader = new ReadReadings();
             }
        }
    }
}
public ReadReadings() {
    try {
        serialPort = (SerialPort) portId.open("ReadReadings", 1000);
    } catch (PortInUseException e) {System.out.println(e);}
    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {System.out.println(e);}
try {
        serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
    serialPort.notifyOnDataAvailable(true);
    try {
        serialPort.setSerialPortParams(9600,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();serialPort.close();
        }
        br = new BufferedReader(new InputStreamReader(inputStream));

    } catch (UnsupportedCommOperationException e) {System.out.println(e);}

    readThread = new Thread(this);
    readThread.start();
}

public void run() {
    try {
        Thread.sleep(10000);
       if(inputStream != null)
        {
            serialPort.close();
            try {
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            serialPort.close();
            ConnectToHardware.frame.dispose();
        frame = new DisplayReadings();
        //frame.pack();
        frame.setVisible(true);

        }

    } catch (InterruptedException e) {System.out.println(e);serialPort.close();} 
}
public void serialEvent(SerialPortEvent event) {
    switch(event.getEventType()) {
    case SerialPortEvent.BI:
    case SerialPortEvent.OE:
    case SerialPortEvent.FE:
    case SerialPortEvent.PE:
    case SerialPortEvent.CD:
    case SerialPortEvent.CTS:
    case SerialPortEvent.DSR:
    case SerialPortEvent.RI:
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        break;
    case SerialPortEvent.DATA_AVAILABLE:
       // byte[] readBuffer = new byte[1024];
       // String temp = "";

        try {
            String inputLine=br.readLine();

            series.put(num, inputLine);
            num++;
            System.out.println(inputLine);

        }

        catch (IOException e) {System.out.println(e);}
        break;
    }


}
}

我需要为我的项目创建可运行的 jar 文件,其中包括 3 个类、外部 rxtx 库和项目中的 librxtxSerial.jnilib。但是在创建可运行的 jar 文件时,此 librxtxSerial.jnilib 文件不包含在 jar 文件中。我认为这是运行 jar 文件时没有得到输出的原因。

【问题讨论】:

  • 显示你的代码,否则 ppl 无能为力
  • 我添加了我的代码但需要创建 jar 文件以便我可以在没有 IDE 的情况下运行。我正在使用日食。我希望双击 jar 文件可以运行我的程序。谢谢。它在 eclipse 中完美运行,但不能运行 jar 文件。

标签: java


【解决方案1】:

JNI 库由操作系统加载。因此,它们不能直接从 jar 文件中加载。它们是从文件系统加载的(有关详细信息,请参阅java.load.path)。

您可以尝试在您的 java 代码中集成一个安装程序,该安装程序在加载本机代码之前将 lib 从 jar 文件复制到文件系统中。

【讨论】:

  • 感谢您的帮助。但我没有得到安装程序的集成部分。哪个安装程序是免费的,因为我作为个人做这个项目,所以我不想花钱为项目购买安装程序。我非常感谢你的帮助。在参数选项卡中包含我的serialLib(exe文件)的路径名的另一件事将解决我的问题。
  • 它不是一个完整的安装程序。我的意思只是一种方法,它将 jni lib 的数据从 jar 文件复制到文件系统。我认为这可以用不到 20 行代码来完成。
  • 您能否给我该代码的链接,因为我想向我的客户提供 jar 文件,以便他可以通过单击而不是通过命令行来使用它。
  • 如果我在本机库文件夹中添加 JNI lib。它会工作吗?如何
猜你喜欢
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 2015-01-15
  • 2014-03-12
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多