come with me, on the way I'll explain.
Arduino and Java
Arduino is a free popular platform for embedded programming based on a simple I/O board easily programmable. Interfacing it with Java allow us to create sophisticated interfaces and take advantages from the several API available in the Java ecosystem.
I’m following the original Arduino and Java interfacing tutorial by Dave Brink but in a more practical approach and with more details.
Step 1) Install the Arduino IDE
This is not a completely mandatory step but it will easy a lot our work. Our program will borrow some Arduino IDE libraries and configurations like which serial port it is using and at which boud rate. At the moment I wrote this tutorial the version of Arduino IDE was 0013.
Step 2) Prepare your Arduino
Connect your Arduino to the serial port in your computer. Here I’m connecting my Arduino with my laptop throught a USB.

Make sure your Arduino IDE is configured and communicating well if your Arduino. Let put on it a little program that sends to us a mensage:
void setup(){ Serial.begin(9600); } void loop(){ Serial.println("Is there anybody out there?"); delay(1000); }
Step 3) Install RXTX Library
We will use some libraries to acess the serial port, some of them relies on binary implementations on our system. Our first step is to install the RXTX library (Java CommAPI) in your system. In a Debian like Linux you can do that by:
sudo apt-get install librxtx-java
Or using a graphical package tool like Synaptic:
For others systems like Windows see the RXTX installation docs.
Step 4) Start a new NetBeans project
Again, this is not a mandatory step but will easy a lot our work. NetBeans is a free and open source Java IDE that will help us to develop our little application. Create a new project at File → New Project and choose at Java at Categories and Java Application at Projects.
Chose a name for your project. I called mine SerialTalker.
At the moment I wrote this tutorial I was using Netbeans version 6.5 and Java 6 update 10 but should work as well on newer and some older versions
Step 5) Adding Libraries and a Working Directory
On NetBeans the Projects tab, right-click your project and choose Properties.
On the Project Properties window select the Libraries on the Categories panel.
Click the Add JAR/Folder button.
Find where you placed your Arduino IDE installation. Inside this directory there’s a lib directory will some JAR files. Select all them and click Ok.
As we want to borrow the Arduino IDE configuration the program needs to know where is they configuration files. There’s a simple way to do that.
Still in the Project Properties window select Run at Categories panel. At Working Directory click in the Browse button and select the directory of your Arduino IDE. Mine is at /home/silveira/arduino-0013.
You can close now the Project Properties window. At this moment in autocomplete for these libraries are enable in your code.
Step 6) Codding and running
Here is the code you can replace at Main.java in your project:
package serialtalk; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import java.io.InputStream; import java.io.OutputStream; 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 talk", 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())); } } } }
Now just compile and run (with your Arduino attached in your serial port and running the program of step 2).
There is. Now you can make your Java programs to talk with your Arduino using a IDE like NetBeans to create rich interfaces.
























2 March, 2009 - 7:32 am
WOW – nice job – now I don’t have to use Micro$oft bloated C#
4 March, 2009 - 6:26 pm
Very nice, but still doesn’t want to work with me, I have: 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:16)
Java Result: 1
Maybe you know why tat’s happend?
5 March, 2009 - 10:28 am
Tom, you installed RXTX library in your system? Seems like the problem I had when it was missing.
7 March, 2009 - 2:52 pm
I installed RXTX as in the instruction, it means, I copied rxtxserial.dll and rxtxparallel.dll to lib folders jre and jdk, and rxtxcom.jar copied to ext folder in Java subfolder in program files, it is all about installation, or maybe I’ve lost something?
7 March, 2009 - 2:54 pm
I use Windows XP.
7 March, 2009 - 3:41 pm
Tom, take a look at
http://rxtx.qbang.org/wiki/index.php/Installation
There’s documentation about how to install it on Windows.
There’s something about copying some .dll to somewhere.
7 March, 2009 - 6:22 pm
Can You write how did You do that? Info from RXTX wiki isn’t easy to follow. Thank you very much
7 March, 2009 - 6:37 pm
Tom, take a look on this:
http://www.jcontrol.org/download/readme_rxtx_en.html
http://www.devmedia.com.br/articles/viewcomp.asp?comp=6722
8 March, 2009 - 5:37 am
Problem is that I’ve done all from http://www.jcontrol.org/download/readme_rxtx_en.html but there s still no change…
12 March, 2009 - 4:16 pm
invalid conversion from ‘const char*’ to ‘long unsigned int’… do you know what exactly would correct this error
16 March, 2009 - 12:44 am
Hello,
I was getting the same problem as Tom, which was”
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:16)”
But then I ran into this post: http://www.arduino.cc/en/Guide/Troubleshooting#toc13.
I then relized that the program that was interfearing with Netbeans was the Arduinos serial monitor. I then closed it and the Serialtaker program ran successfully with no errors.
16 March, 2009 - 10:58 am
@Victor Good tip. You can only open one serial input/output stream at time. You can’t run the Arduino IDE monitor or Arduino uploader and this program at the same time.
16 March, 2009 - 11:49 pm
aggie said:
“invalid conversion from ‘const char*’ to ‘long unsigned int’… do you know what exactly would correct this error”
change the delay(“1000″) to delay(1000).
What’s happening is the delay function is trying to cast a string (I’m assuming a pointer to a character array, I’m not big on Java) as a long unsigned int.
17 March, 2009 - 1:19 am
HI I AM DESIGNING AN A REAL ALARM CLOCK WHICH NEEDS JAVASCRIPT IS IT POSSIBLE TO TRANSFER JAVASCRIPT INFO THROUGH ARDUINO SO THAT THIS ALARM CLOCK DOES WHAT I WANT? I DONT KNOW WHERE TO BEGIN!
28 March, 2009 - 5:18 am
I’m trying to create a GUI with this using Swing in Netbeans.
I created a JForm and put in a text box. Now I want to update the text in the text field from the serial communication.
I created a method and used this to convert to String:
String text = Integer.toString(input.read());
But now how do I invoke textField1.setSet()? Where do I invoke it? I tried creating a method, and invoking it there but I’m getting no love. I tried invoking it from main() but I get “non-static member cannot…from static etc” <-hate this
Haven’t programmed in Java for 2 years so I’m still fresh.
5 April, 2009 - 6:30 pm
Thanks! This was just what I needed.
9 May, 2009 - 4:29 am
Hi…very nice job…it’s what I was looking for….but I can’t understand why I’ve got this errors:
…./arduino/Main.java:28: ‘)’ expected
while(input.available()>0) {
…./arduino/Main.java:28: not a statement
while(input.available()>0) {
…./arduino/Main.java:28: ‘;’ expected
while(input.available()>0) {
Thanks CIAO..
9 May, 2009 - 1:33 pm
Claudio, was a problem in the post encondig. Fixed.
9 June, 2009 - 12:53 pm
Hey, thanks for the info!!
I would like to make a servo run with java, does anyone know where to start, or what kind of commands I need to power the motor?
Cheers,
NLStitch
P.S:
I have some simple C code from arduino.cc, which does the job, but I want to do this in JAVA:
#include
Servo myservo;
int pos=90;
void setup()
{
myservo.attach(9);
}
void loop()
{
myservo.write(pos);
delay(5);
pos=constrain(pos,0,180);
pos++
if(pos > 170)
{
pos = 90
}
}
9 June, 2009 - 1:54 pm
Hi Marijin, I create my own methods for controling the servo. The default servo library doesn’t worked with me.
9 June, 2009 - 1:56 pm
But I will put here in the blog here all the code. I guess I already did it, but if not, I will.
28 June, 2009 - 4:22 am
Hi,
I want to create a graph showing the result of my data which was saved in mysql database. Please,how can I make it happen using netbeans?
7 November, 2009 - 5:14 pm
With Arduino 0017 The problem is Preferences.init() now needs a path to “preferences.txt”. If I give it throws that init() has protected access and doesn’t want compile.
8 November, 2009 - 1:37 pm
Thanks for this tutorial. It was very helpful to me as I am a Java newbie.
But, I’m using Arduino 017 so I got totally tripped up on that Preferences.init().
My solution was simply to remove it all together and just set the values directly.
So, for any other newbies to Java that want to try this, simply remove these 2 lines:
import processing.app.Preferences;
System.out.println(“Using port: ” + Preferences.get(“serial.port”));
Then set the port directly with a string:
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(“/dev/ttyUSB0″);
and the speed with an int:
port.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
Hope that helps any other newbies that get tripped up on this.
There are other solutions and an explanation here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1251256638
It’s a great tutorial and everything is great!
25 December, 2009 - 9:45 pm
Хм… А по-моему, что минусы в данном случае намного превосходят плюсы.
27 December, 2009 - 4:01 pm
Уважаемые, а нельзя оставлять комментарии по теме, а не разную глупость типа Автор молодец и т.д.
10 February, 2010 - 11:00 pm
Thank you for this tutorial.
Everything works fine but everytime I hook up the connection to the arduino it does an automatic reset. How can I fix this?