-1

I am using RXTX(gnu.io.*;) for serial port communiction im my application, I have downloaded the library and added the supporting files as follows:

  1. Copied rxtxParallel.dll and rxtxSerial.dll inside C:\Program Files\Java\jdk1.8.0_25\jre\bin
  2. Copied RXTXcomm.jar inside C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext

I am trying to execute my program mentioned below:

import gnu.io.*;
import java.io.*;
import java.util.*;

public class SimpleWrite implements Runnable, SerialPortEventListener {

    public void run() {
    }
    static Enumeration portList;
    static CommPortIdentifier portId;
    static String messageString = "AAA";
    static char ch = '"';
    static String dest = ch + "XXXXXXXXXXX" + ch;  // 11 Digit Mobile Number.
    static InputStream inputStream;
    static SerialPort serialPort;
    static OutputStream outputStream;

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: {

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            try {

                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                System.err.println("Error while reading Port " + e);
            }
            break;

        }
    } //switch
}

public SimpleWrite(SerialPort serial) {
    try {
        inputStream = serial.getInputStream();
        try {
            serial.addEventListener(this);
        } catch (TooManyListenersException e) {
            System.out.println("Exception in Adding Listener" + e);
        }
        serial.notifyOnDataAvailable(true);

    } catch (Exception ex) {
        System.out.println("Exception in getting InputStream" + ex);
    }

}

public static void main(String[] args) {
    String line1 = "AT+CSMS=1\r\n";
    String line2 = "AT+CMGS=" + dest + "\r\n";
    String line3 = messageString + "\r\n";

    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (portId.getName().equals("COM3")) {
                System.out.println("SMS Sending....Port Found");
                try {
                    serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                    SimpleWrite wr = new SimpleWrite(serialPort);

                } catch (PortInUseException e) {
                    System.out.println("Port In Use " + e);
                }
                try {
                    outputStream = serialPort.getOutputStream();
                } catch (IOException e) {
                    System.out.println("Error writing to output stream " + e);
                }
                try {
                    serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                } catch (UnsupportedCommOperationException e) {
                }
                try {
                    outputStream.write(line1.getBytes());
                    outputStream.write(line1.getBytes());
                    outputStream.write(line2.getBytes());
                    outputStream.write(line3.getBytes());
                    outputStream.write(26);
                    outputStream.flush();
                } catch (Exception e) {
                    System.out.println("Error writing message " + e);
                }
            }
        }
    }
}

/**
 * show text in the text window
 *
 * @param Text text string to show on the display
 */
public static void showText(String Text) {
    System.out.println(Text);
  }
}

But I am getting the error as follows while running my application as follows:

C:\>javac SimpleWrite.java

C:\>java SimpleWrite 
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while
loading gnu.io.RXTXCommDriver
Exception in thread "Thread-0" java.lang.UnsatisfiedLinkError: no rxtxSerial in
java.library.path
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
        at SerialConnection.openConnection(SerialConnection.java:71)
        at Sender.send(Sender.java:46)
        at SMSClient.run(SMSClient.java:39)
        at java.lang.Thread.run(Unknown Source)

I have checked but all the solutions is showing is regarded with IDEs. How can solve this? Please guide me I am new to this!

Santhucool
  • 1,656
  • 2
  • 36
  • 92

1 Answers1

1

If you are running the program from IDE, make sure that your project's java platform is set to the one you copied library, i,e, C:\Program Files\Java\jdk1.8.0_25\

Because, IDE comes with default java platform & when you run the project it uses default Java platform.

You can change which platform to use for your project under project's properties dialog.

  • buddy, I am not using an IDE. Please help me how I can solve this without IDE. I am lover of CMD, I dont prefer IDEs – Santhucool Jan 08 '15 at 06:09
  • so, I assumed that your JDK is installed on C:\Program Files\Java\jdk1.8.0_25\. Make sure that your JAVA_HOME is set to C:\Program Files\Java\jdk1.8.0_25\. Whenever you execute the java program by java.exe, make sure that it points to %JAVA_HOME\bin\java.exe –  Jan 08 '15 at 07:38
  • Or, try manually setting java.library.path when you issue java.exe on command line. Example. java.exe YourJavaClass -Djava.library.path="path/to/librxtx/installed/directory" –  Jan 08 '15 at 07:43
  • My RXTXcomm.jar path is C:\ProgramFiles\Java\jdk1.8.0_25\jre\lib\ext and I run using the command java SimpleWrite . Can you tell how should I run the program? Please help buddy!! – Santhucool Jan 08 '15 at 09:20
  • Hi, try this. java -cp "C:/ProgramFiles/Java/jdk1.8.0_25/jre/lib/ext/RXTXcomm.jar;." -Djava.library.path="C:/ProgramFiles/Java/jdk1.8.0_25/jre/bin" SimpleWrite –  Jan 09 '15 at 01:29
  • one more point, If you copy the dll & jar to the location as suggested by RXTX installation guide, no need to mention -cp & java.library.path. Still if you get the UnsatisfiedLinkError, it means that you copied it to different Java platform, and running your program using another java platform. Make sure that you are reffering the correct one. –  Jan 09 '15 at 01:39