Java Comm Serial API How-To for Linux

Using RXTX and JCL

Preface

Special thanks to Trent Jarvi and Kevin Hester for putting together RXTX and JCL.  These two gentlemen have done an excellent job of porting the original Sun Java Comm API to the Linux OS.  However, with all due respect, and not to be insulting, their installation documentation leaves much to be desired.  This is the primary reason I have chosen to put this document together.

Getting Started

Before insalling the Comm API on your Linux box, be sure you have a JSDK installed.  You can obtain a JSDK for your version of linux from Blackdown.org or Sun Microsystems.  To install and configure Java in Linux, please follow the instructions provided here.

For the purposes of this paper, it will be assumed that you have installed a JSDK and it is located in:

/usr/java/j2sdk1.4.0/

And java is then located in:

/usr/java/j2sdk1.4.0/bin/

If your setup is different, please adjust accordingly. 

Installing RXTX

First, obtain the RXTX bins package from:

http://www.linux.org.uk/~taj/rxtx-bins.1.tar.gz
                        (In IE, right click and "Save Target As")

Decompress and Untar this package:

/bin/gzip --decompress rxtx-bins.1.tar.gz

/bin/tar xf rxtx-bins.1.tar

At this point, you'll have an rxtx-bins.1 directory.  Next, you'll need to copy the shared objects into your java installation:

cp rxtx-bins.1/1.4/i386-pc-linux/libParallel.so /usr/java/j2sdk1.4.0/jre/lib/i386/

cp rxtx-bins.1/1.4/i386-pc-linux/libSerial.so /usr/java/j2sdk1.4.0/jre/lib/i386/

If you are installing on an architecture other than an x86, you'll need to adjust both the /i386-pc-linux/ and the /i386/ accordingly.

Next, you'll need to install the jcl.jar file:

cp rxtx-bins.1/1.4/jcl.jar /usr/java/j2sdk1.4.0/jre/lib/ext/

At this point, the RXTX installation is complete.

Installing Comm

The final step to getting the Java Comm API working under Linux, is to install the Comm API itself.  At this point, you have all of the necessary kernel-level drivers installed.  Because Linux is a form of Unix, the authors of RXTX and JCL have wisely decided to reuse sun's solaris (unix) comm library.  At this point, you must download and install this library:

http://java.sun.com/products/javacomm/  

Make sure you choose the Solaris/SparcTM version.  Next, you must Decompress and Untar this package:

/bin/gzip --decompress javax_comm-2_0_2-solsparc.tar.Z

/bin/tar xf javax_comm-2_0_2-solsparc.tar

Next, you'll need to install the comm.jar file:

cp commapi/comm.jar /usr/java/j2sdk1.4.0/jre/lib/ext/

At this point, we are almost finished.  We just need to create the properties file that the Comm API will use to load the drivers (.so files).  To create this file, type the following command:

/bin/echo Driver=gnu.io.RXTXCommDriver > /usr/java/j2sdk1.4.0/jre/lib/javax.comm.properties

Congratulations!  You have installed the Linux Comm API.

Tips

While windows uses COM and LPT designators for port identifiers, Linux is a bit different.  Use the following table to identify your ports:

Port Windows port identifier What you use in Linux
Serial Port 1 COM1 /dev/ttyS0
Serial Port 2 COM2 /dev/ttyS1
Parallel Port 1 LPT1 /dev/lp0

Troubleshooting

Several people have emailed me and reported that they get the following error:

Exception in thread "main" java.lang.VerifyError: (class:
gnu/io/RXTXPort$SerialOutputStream, method: write signature: ([BII)V) Illegal
use of nonvirtual function call
at gnu.io.RXTXPort.<init>(RXTXPort.java)
at gnu.io.RXTXCommDriver.getCommPort(RXTXCommDriver.java)
at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:547)


I personally have not experienced this problem, but if you encounter this error, you can work around it by adding -noverify to your command line:

java -classpath your classpath -noverify your class

Also, it has been suggested that recompiling the jcl with the java compiler you have chosen will resolve this issue.  As I have never experienced this verification problem, I have no way to test this.  It should also be noted that the purpose of this paper is to be a quick and easy way to get serial and parallel port access in Java on Linux.  Most of the instructions on this page as well as the VerifyError are irrelevant if you chose to download the RXTX source and do a manual compile and installation.

Other Resources

  1. Trent Jarvi's RXTX Homepage.  (http://www.rxtx.org)
  2. Kevin Hester's JCL Homepage.  (http://www.geeksville.com/~kevinh/linuxcomm.html)
  3. Sun Microsystem's Java Comm FAQ.  (http://java.sun.com/products/javacomm/reference/faqs/index.html)

Reader Comments:

07/01/2014 6:10 AM - Jason wrote:
good article

06/09/2014 2:57 AM - Pharma908 wrote:
Very nice site!

05/31/2014 2:36 PM - Johnk441 wrote:
Hi! Someone in my Myspace group shared this website with us so I came to look it over. I'm definitely loving the information. I'm bookmarking and will be tweeting this to my followers! Great blog and terrific design and style. gafcaddeaaka

03/31/2013 3:31 PM - Jub16 wrote:
I don't know rxtx. ..I want to use in my project. .tell me from where to get started for beginners.

10/10/2012 11:38 AM - amjad wrote:
hi ,i use the javax.comm packet and run correctly, the question is how make my file to be .jar file, i use the old way like i write the mianfest.txt file and also don't work

10/08/2012 10:22 PM - Conny wrote:
Smart thinking - a cleevr way of looking at it.

07/14/2012 10:19 AM - Tom wrote:
<b>Nice article</b>

06/19/2012 4:24 PM - Camilo Duque wrote:
Hi, excellent content. I have a simple question: I'm using a laptop without rs232 serial port, instead I'm programming with rxtx through a USB port, so I need to know if the internal manage of communication is made by the kernel or rxtx driver or JVM? and I wonder if the real transmission occurs in RS232 or in USB protocol? Thanks a lot and congrats... Camilo

02/09/2012 11:16 AM - Alex wrote:
You saved me....

11/22/2011 11:17 PM - Nelson wrote:
Yap Nice job

11/19/2011 2:23 PM - Sam Ong wrote:
I have a Nomad Trimble with Windows Mobile. I am trying to connect it through the serial port. Can I use Linux on it as well?

11/10/2011 5:51 AM - nuwan wrote:
let me know how to download javax.comm api suitable for linux

09/15/2011 2:53 PM - greg wrote:
I get it now. You can't put any html in this comment system or it rejects the email without an error. The link to the comm code does not have a download. Seems that Wade needs to edit the page so that it reflects this? Maybe a link to where the code is now?

09/15/2011 2:50 PM - greg wrote:
Is this comment system broke too? I am trying to find the javax.comm api code. The link provided does not have a download. Does anyone know where I can get this download? The page was updated 9 days ago and I figure that oracle broke it.

06/11/2011 3:06 PM - Z.K. wrote:
Just curious, but if you are using Linux why not download the Linux version of the Java Comm API.

01/03/2011 11:42 PM - karan nage wrote:
i got problem at the time of implementation javax.comm for parallel port error is javax.comm class path were not found if any body hava solution plz post me lioninfotechhr@gmail.com contact no 9637624887

01/01/2011 8:13 AM - mitra wrote:
ops, I did the instruction but still same error like before while im sure about classpath, error: Exception in thread "main" java.lang.NoClassDefFoundError: net/tinyos/sf/SerialForwarder Caused by: java.lang.ClassNotFoundException: net.tinyos.sf.SerialForwarder 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:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) Could not find the main class: net.tinyos.sf.SerialForwarder. Program will exit. any help? thanks in advance.

12/13/2010 7:02 AM - bart wrote:
hi. i did almost the same in windows and it seems to work fine. the clue is: /bin/echo Driver=gnu.io.RXTXCommDriver > /usr/java/j2sdk1.4.0/jre/lib/javax.comm.properties... this file is in ProgramFiles/Java/jre6/lib.. Project configured with rxtx and javax.comm as UserLibraries and added to buildPath in eclipse works fine

10/01/2010 8:16 AM - Valerione wrote:
hello, I followed the guide! I tried to run a little code to test the proper operation, but gives me this error: xception in thread "main" java.lang.UnsatisfiedLinkError: com.sun.comm.SunrayInfo.isSessionActive()Z at com.sun.comm.SunrayInfo.isSessionActive(Native Method) at com.sun.comm.Portmapping.registerCommPorts(Portmapping.java:155) at com.sun.comm.Portmapping.refreshPortDatabase(Portmapping.java:100) at javax.comm.CommPortIdentifier.<clinit>(CommPortIdentifier.java:138) at Coordinator.Main.main(Main.java:15) Java Result: 1

10/31/2009 2:00 PM - SchneiderIS wrote:
Pardon the blond moment. The permissions to write to the "lib" folder were needed. Then the file could be created.

10/31/2009 1:50 PM - SchneiderIS wrote:
In running your instructions I have run into a snag with the very last step for the Sun comm driver. When I try running: sudo /bin/echo Driver=gnu.io.RXTXCommDriver > /usr/lib/jvm/java-6-sun/jre/lib/javax.comm.properties I get the following error: bash: /usr/lib/jvm/java-6-sun/jre/lib/javax.comm.properties: Permission denied Looking in the "lib" folder there is no "javax.comm.properties" and when I search the drive for the file it does not exist. I have the latest download from Sun which is version 3 for comm and my java version is as follows: java version "1.6.0_16" Java(TM) SE Runtime Environment (build 1.6.0_16-b01) Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing) Do you have any suggestions as to what needs to be changed for this final command to register the driver?

05/10/2009 8:01 PM - Rzhbkqcf wrote:
Excellent work, Nice Design

11/19/2008 4:38 AM - ng_agi wrote:
it would be great if you have something similar for windows. there are a couple of words in wiki about rxtx win32 installation under eclipse, but i am still fighting with my installation...

11/18/2008 9:02 AM - MRI wrote:
the www.rxtx.org site wiki, and most of the links no longer lead to active sites. Is there a place to get the source for rxtx. I tried accessing the cvs site and that as well no longer functions. Advice?

08/21/2008 3:10 AM - Rajan wrote:
Well it works on linux. I need it to make it work on MAC OS(10.4). Will try to email you with some details, if you could provide any advise and help, would be appreciated.

08/07/2008 8:52 AM - WW wrote:
Point well taken, I've listened, and I've changed the site's text color :)

08/07/2008 2:54 AM - Claus wrote:
This website is hard to read, due to lightgray text on white background. Please, change this.

Post a Comment:

Your Name:
Your Comment:

Copyright 2002-2014 Wade Wassenberg
Last Modified: March 24, 2004. 
If you have questions, comments, or if you find any part of this document to be inaccurate or incorrect, please submit your feedback to webmaster@wass.homelinux.net.