Interfacing Java with Arduino

Hi, this is my first post. I have Arduino a few days, but I still can't connect it and send any data to Arduino or receive it from Arduino using Java. I've tested RXTX and JavaComm but still there is nothing working, so maybe you will help me.

This is first version of my code:

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.*;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.RXTXCommDriver.*; 
import processing.app.Preferences;

public class Main {

    static InputStream input;
    static OutputStream output;

      public static void main(String[] args) throws Exception{

            Preferences.init();
            System.out.println("Using port: " + Preferences.get("serial.port"));

        System.out.print(CommPortIdentifier.getPortIdentifiers().toString());
      }

}

and output:

run:
Using port: COM4
java.lang.NoClassDefFoundError: javax/comm/CommDriver thrown while loading gnu.io.RXTXCommDriver
java.lang.NoClassDefFoundError: javax/comm/CommDriver thrown while loading gnu.io.RXTXCommDriver
gnu.io.CommPortEnumerator@5740bb
BUILD SUCCESSFUL (total time: 3 seconds)

There is also second version:

package arduino2;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.*;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.RXTXCommDriver.*; 
import processing.app.Preferences;

public class Main {

    static InputStream input;
    static OutputStream output;

      public static void main(String[] args) throws Exception{

            Preferences.init();
            System.out.println("Using port: " + Preferences.get("serial.port"));

        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(Preferences.get("serial.port"));
        
            SerialPort port = (SerialPort)portId.open("serial madness", 4000);
        input = port.getInputStream();
        output = port.getOutputStream();
        port.setSerialPortParams(
                        Preferences.getInteger("serial.debug_rate"),
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);


            while(true){
                  while (input.available()>0)
                        System.out.print((char)(input.read()));
            }
      }

}

and output:

Using port: COM4
java.lang.NoClassDefFoundError: javax/comm/CommDriver thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" gnu.io.NoSuchPortException
        at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:218)
        at arduino2.Main.main(Main.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

With Arduino software all works fine, i don't understand why it doesn't with my soft. In Netbeans project are all .dll files, all Jar libraries from arduino lib folder and also preferences.txt .

But still no effect. Thanks for any help, maybe someone can upload working netbeans project, so i can try it out, maybe my laptop isn't working good, i'm looking errors all day long. Forgive me my english.

Hi TommyBar,

The java.lang.NoClassDefFoundError error is indicative of the JVM not being able to find a class within a package you’ve imported. So, in your second code example and related exception, gnu.io.CommPortIdentifier.getPortIdentifier can’t be found. Often times the solution to his is just putting the package jar in the correct location so the JVM can find it, or updating the CLASSPATH to include the location of that package.

My suggestion is to make some changes to the location of the gnu.io.RXTXCommDriver package (I think it’s likely a file named RXTXcomm.jar) to ensure it is in the /lib directory of your JVM home. I hope this is helpful - give us an update if you get it figured out!

Mark~

Problem is that I've done all intstructions from internet and there is still no change, maybe anybody can give me link to working tutorial? Maybe there is still something to do for me.