2016-10-26 151 views
0
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在project.But創建可運行jar文件時,該librxtxSerial未包含在jar文件中的.jnilib文件。我認爲這是運行jar文件時沒有得到輸出的原因。需要幫助獲得librxtxSerial.jnilib進入可運行jar文件

+0

顯示您的代碼,否則ppl不能幫助 –

+0

我添加了我的代碼,但需要創建jar文件,以便我可以在沒有IDE的情況下運行。我正在使用eclipse。我想雙擊jar文件將運行我的程序。謝謝。它在eclipse中完美工作,但不能運行jar文件。 – krishna

回答

0

JNI庫由操作系統加載。因此,它們不能直接從jar文件加載。它們從文件系統加載(有關詳細信息,請參見java.load.path)。

您可以嘗試將安裝程序集成到您的java代碼中,該代碼會在加載本機代碼之前將lib文件中的lib複製到文件系統中。

+0

感謝您的幫助。但我沒有得到集成安裝程序的一部分。安裝程序是免費的,因爲我作爲個人做這個項目,所以我不想花錢買項目的安裝程序。我真的很感謝你的幫助。還有一件事包括我的serialLib(exe文件)在參數選項卡的路徑名稱將解決我的問題。 – krishna

+0

這不是一個完整的安裝程序。我的意思只是一個方法,它將jni庫的數據從jar文件複製到文件系統。我認爲這可以用少於20行的代碼完成。 – clemens

+0

你可以給我的代碼鏈接,因爲我想提供jar文件到我的客戶端,以便他可以通過點擊它而不是通過命令行來使用它。 – krishna