rxtx取代javax.comm实现Java跨平台设备端口通信

Using RXTX

From Rxtx

<!---->

This page is for general content regarding the use of rxtx. Feel free to add your own content.

I wrote an app several months ago using javax.comm on windows. Sun has left me high and dry. rxtx help!

  • download ftp://ftp.qbang.org/pub/rxtx/rxtx-2.1-7-bins-r2.zip
  • unzip it
  • copy rxtxSerial.dll into your c:\program files\java\jre-version\bin dir
  • copy RXTXcomm.jar into your c:\program files\java\jre-version\lib\ext dir
  • change all references from 'javax.comm' to 'gnu.io'
  • recompile
  • test
 Dido Sun left me Hi and dry. After 30+ hours wasting my life on
javax.comm it is the RXTXcomm 2.1.7 that really worked the first 
time
 REMEMBER !! for the newbies
  Change the all occurences of the import javax.comm.*; in  
   MyApps.java
  TO import gnu.io.*;
 I did not even have to recompile as suggested the rxtxSerial.dll
 a job well done for the rxtx.org team.
 Even after following the above instructions, under Netbeans 5.5 on Windows, I needed to right-click Libraries
under Projects on the left hand side of the screen, and select Add JAR/Folder, picking the RXTXcomm.jar file.
 Only then did my "import gnu.io.*;" become a valid command.  Also, I got a run-time error where it failed to
load the rxtxSerial.DLL library because it couldn't find it.  I needed to right-click the project name, select
properties, select Run under categories, and ...

add a VM Options: entry of -Djava.library.path="C:/where-ever-you-stuck-rxtxSerial.DLL-file;%PATH%" While I'm sure there is more than one way of adding the directory to your path, this worked for me. Note I did the default NetBeans 5.5/JDK 1.6.0 isntall accepting all the default options(ie clicking next until I got to finish), so I haven't modified my java paths or anything.  

 

Using RXTX In Eclipse

From Rxtx

<!---->

This is how I add and use RXTX in Eclipse for Win32 Projects, there are probably other ways but it works for me. mailto:[email protected]

  1. Copy RXTXcomm.jar file and dll files to the lib directory of your project
  2. Under Project | Properties | Java Build Path | Libraries
  3. click Add JARs... Button
  4. Select the RXTXComm.jar from lib directory
  5. Jar should now be in the Build Path
  6. expand the RXTXComm.jar entry in the list and select "Native Library Location"
  7. Select the project lib directory and apply

Be careful when using System.in.read() and rxtx in win32; It can trip across a known JRE deadlock (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4809647) bug

Deploying JAVA with RXTX

From Rxtx

<!---->

Saving the RXTX jar-file and DLL in the JRE is not optimal if you indend to distribute your JAVA application and do not want to bundle it with a customized JRE.

[ edit]

Windows

For Windows, the Janel (http://sourceforge.net/projects/janel) JAVA launcher can be used.

  • create a directory for your application, and save the Janel executable and lap-file to it;
  • create a subfolder named "classes" and save the jar-file of your application as well as the RXTXcomm.jar in it;
  • create a subfolder named "lib" and save the rxtxSerial.dll to it;
  • make the following entries in the lap-file:
    • "-Djava.class.path=;${SELF_HOME}\classes\YourApp.jar;${SELF_HOME}\classes\RXTXcomm.jar;"
    • "janel.main.class=YourAppMainClass"
    • "janel.library.path.dir=${SELF_HOME}\lib"
[ edit]

unjar

It should be possible to unjar the RXTXcomm.jar and then add the .classes into YourApp.jar. This should eliminate the secon .jar and the need to modify java.class.path. Please clarify the steps if you pursue this.

I was successful in doing this on my Mac. First, unzip the RXTX.jar file. I didn't try unjar, as unzip worked fine. Then, place the gnu folder in the same folder as your application jar file. Execute the following command from the command line:

jar uvf Your_Apps_Name_here.jar gnu

to update the jar with the contents of the classes in the gnu folder. Your app will now run by double clicking on it, provided the .dll or .jnilib library is in the same folder. I would love to include the OS specific libraries in the jar for a single, monolithic package, but after researching this on the web, it is apparently very difficult at best and probably impossible.

[ edit]

Other Platforms

It would be great if someone with access to Linux boxes could provide us with similar recipes for their operating system.

The above unjar instructions and subsequent jars worked fine for my app on both Mac and Windows machines.

<!---->
Wonderful: 推荐rxtx包,可以完美替代javax.comm,目前javax.comm在windows下的开发维护已经停止了,rxtx的旧版本支持在javax.comm-win32-2.0基础上的扩展,rxtx新版本支持对javax.comm的覆盖式支持,也就是说原来用javax.comm的把所有import javax.comm.*改成import gnu.io.*就可以正常使用了,其他只须相关的dll文件,不用properties文件,支持的端口类型也明显多了很多,请看: http://rxtx.org/
两个库都可以在Eclipse下随项目跑,相信在纯项目中也可以做到。
看我的blog,里面有相关介绍 http://blog.csdn.net/djsl6071/category/296859.aspx

你可能感兴趣的:(java,eclipse,windows,linux,Netbeans)