33

I'm trying to write a java application that accesses the usb ports to read from and write to a device connected through usb. The problem I face is that I don't know what exactly to use in java to do such a thing. I searched online and found something called JUSB but all the posts seem fairly old.

Currently I'm using the RXTX libraries but I sometimes run into some sync error. When I use C# to do the equivalent it requires far less code and I don't face any of the same sync error.

My question is, is there anything built into the latest version of the JRE I can use to access the usb ports (that is just as easy as the equivalent C# code)?

progyammer
  • 1,498
  • 3
  • 17
  • 29
Petezah
  • 1,465
  • 4
  • 26
  • 30

6 Answers6

27

Been using usb4java for a year on cross platfom (Linux and Windows) and it works great.

See:

http://usb4java.org/

They are very active and have a very good javax USB front.

jboisvert
  • 271
  • 3
  • 2
14

There is nothing equivalent to C#'s USB support in Java. Both jUSB and Java-USB are severely out-of-date and likely unusable for any serious application development.

If you want to implement a cross-platform USB application, really your best bet is to write an abstract JNI interface that talks to Linux, Mac and Windows native libraries that you'll have to write yourself. I'd look at LibUSB to handle Mac and Linux. Windows, as you've seen, is pretty straightforward. I just came off a year-long project that did just this, and unfortunately this is the only serious cross-platform solution. If you don't have to implement on Windows and your needs are limited, you may get by with one of the older Java libs (jUSB or Java-USB). Anything that needs to deploy on Win32/Win64 will need a native component.

Dave Sims
  • 5,060
  • 3
  • 24
  • 26
  • What about [usb4java](http://usb4java.org/)? Seems to be a good alternative. I am not related to this development. – Brethlosze Jul 30 '19 at 00:50
7

You might want to have a look at usb4java - http://usb4java.org/index.html it appears to support Windows, Linux and Mac OS's and appears to be reasonably current at the time of posting. Is unfortunately under the LGPL so may not be suitable for commercial development.

wombling - Chris Paine
  • 1,651
  • 1
  • 18
  • 17
  • 2
    Linking with LGPL libraries should not be an issue, if you are going to only use their APIs in your code (and you have a non LGPL license). GPL licensed libraries are copyleft licenses, forcing your code's license also to be GPL license, regardless of what license you have used. – Joseph Jun 16 '14 at 10:59
7

See the jUSB or usb4java libraries.

ThunderPhoenix
  • 1,649
  • 4
  • 20
  • 47
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
  • 5
    Java-USB is Linux-only. If that's all you need, it may work out, although it doesn't appear to have been updated in a while. – Dave Sims Jan 23 '10 at 01:09
2

The Java Communications API. This should provide similar functionality to the C# System.IO.Ports namespace.

Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
  • 1
    System.IO.Ports is for Serial communications unless the OP is implying a serial-to-usb interface?! – t0mm13b Jan 23 '10 at 00:51
  • Agreed -- System.IO.Ports has no USB interfaces, is only for Com Ports. – Dave Sims Jan 23 '10 at 01:02
  • If you're USB device is exposed on a COM port then you can use System.IO.Ports. – Taylor Leese Jan 23 '10 at 01:24
  • Taylor: Have you tried it? Does it actually work? – t0mm13b Jan 23 '10 at 01:30
  • 1
    Yes, I have an application that already does this that I use every day. The USB device is an encoder for magnetic stripe cards. – Taylor Leese Jan 23 '10 at 01:34
  • So that's a USB to serial cable? unless the USB driver is using serial emulation...interesting...never heard of that angle before...I have used a magnetic card reader which is a serial interface, using a usb cable... but it does not work (the limitation on the driver itself plus the cable was cheap) – t0mm13b Jan 23 '10 at 01:49
  • It is using a USB-RS232 serial COM port interface, but there is no actual USB to serial cable connected to the devices USB cable. – Taylor Leese Jan 23 '10 at 02:09
-2

If you're on a mac, USB's can be accessed with the /Volumes/ directory.

Example:

You want to write to a file in "Drive", so the filepath would be:

/Volumes/Drive/file.whatever

Not sure how it's accomplished on other platforms, but this is the simplest way I've found on a mac

Linny
  • 818
  • 1
  • 9
  • 22