2

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

Build path

How do I get the program to execute properly with the jssc.jar in the path?

mightymouse3062
  • 109
  • 5
  • 13
  • Sounds like you want to make sure the SerialCommunication.class gets into YOUR jar and not necessarily the jssc.jar. Right? If you want to execute your executable jar, then put the SerialCommunication.class into your jar, whatever it's name is. – mikemil Feb 28 '14 at 20:15
  • 1
    It is a valid file, I tried to get this to work on an OS X box by putting jssc.jar in /Libraries/Java/Extensions and it worked without a problem. When I try to include it in my project it will build without a problem but when it runs, automatically hits the error above – mightymouse3062 Feb 28 '14 at 21:43
  • I don't understand "I have added jssc.jar to my build path but when I run the program"? Do you mean "classpath" instead of "build path"? If yes, how did you add it? – Peter Keller Mar 01 '14 at 10:28
  • I added it to the build path like this http://stackoverflow.com/questions/18870213/adding-external-jar-to-eclipse – mightymouse3062 Mar 02 '14 at 14:50

1 Answers1

0

Is the JRE you are using for running the program >= than the JDK with which jssc.jar was compiled?

robermann
  • 1,722
  • 10
  • 19