I am working on a project that is going to do serial communication and I want to package the java-simple-serial-connector (jssc) as part of the project.
I don't want to install the jssc.jar file on the system because this project is going to be run a bunch of different machines so it would be much easier to package the .jar with my executable .jar.
I have added jssc.jar to my build path but when I run the program (note it isn't finished yet) I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: SerialCommunication
Caused by: java.lang.ClassNotFoundException: SerialCommunication
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Here is my code
import jssc.*;
public class SerialCommunication {
public static void main(String[] args) {
String[] portNames;
if(System.getProperty("os.name").equals("Mac OS X")){
portNames = SerialPortList.getPortNames("/dev/");
System.out.println("OS X");
} else {
portNames = SerialPortList.getPortNames("COM");
System.out.println("Windows");
}
for (int i = 0; i < portNames.length; i++) {
System.out.println(portNames[i]);
}
SerialPort serialPort = new SerialPort(portNames[0]);
try {
serialPort.openPort();// Open serial port
serialPort.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.writeBytes("This is a test string".getBytes());
serialPort.closePort();// Close serial port
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
}
Under the build path I added the jssc.jar as a library and as an external library. Both of them fail in the same way. I have also cleaned the solution.
EDIT:
My file structure is the following
/src
SerialCommunication.java
/lib
jssc.jar
Here is my build path
How do I get the program to execute properly with the jssc.jar in the path?