Java supports communication to serial ports, but not with its default installation. It requires an installation of an external library. Currently, two options exist for achieving serial communication:
Unfortunately, the current version of JavaComm does not support Windows, and only supports Solaris SPARC, Solaris x86, and Linux x86. Since I use none of those operating systems, this article will only discuss how to install RXTX in Windows.
UPDATE: 12 Sept 2011 – Thanks to a comment from Kurt Zoglmann, there are instructions on how to do this with a Mac! I never tried this myself because I don’t have a Mac, but the procedures stated there look good to me (see comments).
A 32 and 64 bit version of the library exists. I’ve updated this article from when it was first published to include the new locations for where to download the libraries, but I have not gone through the installation of the libraries again, so I’m not surprised if the rest of the content on this page is outdated too.
32-bit version: http://rxtx.qbang.org/wiki/index.php/Download
64-bit version: http://fizzed.com/oss/rxtx-for-java
To install the libraries (instructions from JControl):
- Copy rxtxSerial.dll to %JAVA_HOME%\bin, (%JAVA_HOME% is the folder where JRE is installed on your system; e.g. c:\Program Files\Java\j2re1.4.1_01)
- Copy RXTXcomm.jar to %JAVA_HOME%\lib\ext
Once installed, the IDE will need to know where to look for these installed files. Even though the files exist in the JRE directory, each project needs to know about these files. More information on this website.
In order to start coding with this library, import gnu.io.*. For more information about Java Serial Programming, look here.
Happy coding!
Kurt Zoglmann 2011-09-12
There doesn’t seem to be a prebuilt 64bit library for OS X. This is a problem with Lion since it only comes with the 64bit version of Java 1.6.0_26. To save someone some hassle download 2.2-pre2 from the official site. Install the free application, Xcode, from the App Store. Then modify the configure script as follows:
JAVAINCLUDEDIR=$JPATH/../../../Headers
Next run the configure script:
./configure
Finally install it by issuing the following command:
sudo cp i386-apple-darwin11.0.0/librxtxSerial.jnilib /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Libraries
Kurt Zoglmann 2011-09-12
There was a formatting problem in my post above. In the configure script you want the JAVAINCLUDEDIR line to be set to the following.
JAVAINCLUDEDIR=/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers
What you see in the above post is what you will find JAVAINCLUDEDIR already set to. That path does not exist on OS X 10.7 (Lion) and hence the build will fail. Manually setting the environmental variable as suggested by the configure script doesn’t seem to work for me.
hp 2011-09-12
Thank you for your input. I had not previously considered the Mac scenario.
hp 2011-09-12
I have also updated the post to let others know that your procedure for Macs is below.
JC Dufourd 2011-09-20
@Kurt: Thanks for the Mac OSX Lion instructions, but they were not enough for me.
I got this error: “glibtool: compile: unable to infer tagged configuration”.
Browsing the web, I saw someone just adding a fake “–tag=junk” to the glibtool line for Mac OSX in configure.
With that, the error disappears and compilation is successful but for a warning.
Kurt Zoglmann 2011-10-18
I was wrong about there not being a 32bit version of Java in OS X Lion. If you want to use a prebuilt 32bit version of RXTX, you will be able to find the prebuilt binary with a quick google. To enable the 32 bit version of Java by default, open the “Java Preferences” application found in /Applications/Utilities. Drag the 32bit version of Java to the top and exit the program. The default java on the command line will now be the desired version and it can be found at /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK .
hp 2011-10-21
Thanks for the update!
Jim Ryan 2011-12-02
JavaComm works fine in Windows (32 bit only, though.) Install:
comm.jar in
JAVA_HOME/lib
JAVA_HOME/jre/lib/ext
win32com in
JAVA_HOME/jre/bin
Windows/System32
javax.comm in
JAVA_HOME/lib
JAVA/jre/lib
But HELP! I’m having trouble installing RXTX in Solaris. I think I’m not putting the librxtxserial.so file in the right place. I’m getting
UnsatisfiedLinkError no rxtxserial in java.library.path.
I’m not adept at Solaris. Any help appreciated.
hp 2011-12-12
Not sure how I can help you here…I’ve never touched Solaris before in my life. Google is probably your best shot. Sorry mate.
Roddy 2012-01-24
Hi!
I’ve download RXTX from download page. After, I copy the file in my Java directory. I start my JCreator (I use this program to do Java) but it don’t find GNU.IO.*
I’ve read at this link http://rxtx.qbang.org/wiki/index.php/Using_RXTX we must change all references from ‘javax.comm’ to ‘gnu.io’
But, where is references from ‘javax.comm’)
hp 2012-01-24
This is referring to the file imports you use in the beginning on the file. Rather than importing Java.comm, one must import gnu.io
Roddy 2012-01-24
I’ve imported gnu.io with the instruction “import gnu.io.*;” but when I compile there is this error: package gnu.io does not exist. What will I do?
hp 2012-01-24
That may be an indication that rxtx was not properly installed.
Roddy 2012-01-24
I know there is a problem with the RXTX installation. But where is the problem?
I download the file calls “rxtx-2.1-7-bins-r2.zip”. I unzip it and copy file calls “RXTXcomm.jar” in Javajre6libext and file calls “rxtxSerial.dll” in Javajre6bin.
Where is my error?
hp 2012-01-24
The project settings need to be altered such that it knows to look for these files. Please refer to the links on this page for more info: http://rxtx.qbang.org/wiki/index.php/Using_RXTX
Shan Salman 2013-02-16
Hi All,
I have installed the rxtx successfully and trying to read some data sent by arduino board (which sends “hello world” 9600 Baud). The code works well and reads the serial data but gives out an exception error “Underlying input stream returned zero bytes” randomly. Please help
Treehouse Projects 2013-03-14
Thank you so much!
yogesh 2016-05-23
RXTXcomm.jar work correctly in Eclipse Mars with Java8 but when i create installer with Installshield2009 then it not able to connect to serial port for communication it required comm.jar , win32.dll and jcomm.properties script file but it become slow with that 3 files.please can u help me how i use this RXTXcomm.jar file with Installshield 2009 ? or it must required those comm.jar , win32.dll and jcomm.properties ?