just get the names of USB devices attached to a system?

17,781

Solution 1

You can use the jUsb API, for Linux.

Or you could launch the terminal in Linux using the Process class, and run ls -la /dev/disk/by-id/usb-* and catch the stdout to know the results.

For Windows, you can try this : How to find my USB flash drive's path with PowerShell

EDIT:

For Windows, another helpful utility is the devcon.exe.

For more info, check this.

EDIT 2: For Mac, you could launch the terminal using the Process class, and run system_profiler SPUSBDataType

Solution 2

Yoy can try javahidapi. I think it some c/c++ code and JNI. Declarated linux, mac and windows support. I have tried it with linux (ok), with clean windows in virtual box (not ok, UnsatisfiedLinkError, i think some MSVS libs was missed). If you'll compile it from source, it should work, i belive.

here is example:

import com.codeminders.hidapi.HIDDeviceInfo;
import com.codeminders.hidapi.HIDManager;

public class TestHid {

    public static void main(String[] args) throws Exception {
        try {
            com.codeminders.hidapi.ClassPathLibraryLoader.loadNativeHIDLibrary();
            HIDManager hidManager = HIDManager.getInstance();
            HIDDeviceInfo[] infos = hidManager.listDevices();
            for (HIDDeviceInfo info : infos) {
                System.out.println("info: " + info.toString());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

EDIT output shows only one plugged in usb device, genius laser mouse.

[grigory@gr testRSA]$ pwd
/home/grigory/testRSA/out/production/testRSA
[grigory@gr testRSA]$ whoami 
grigory
[grigory@gr testRSA]$ java -cp ".:hidapi-1.1.jar" Test
libusb couldn't open USB device /dev/bus/usb/003/002: Permission denied.
libusb requires write access to USB device nodes.
info:HIDDeviceInfo [path=0003:0002:00, vendor_id=1112, product_id=58, serial_number=null, release_number=0, manufacturer_string=null, product_string=null, usage_page=0, usage=0, interface_number=0]
[grigory@gr testRSA]$ sudo java -cp ".:hidapi-1.1.jar" Test
[sudo] password for grigory: 
info:HIDDeviceInfo [path=0003:0002:00, vendor_id=1112, product_id=58, serial_number=null, release_number=0, manufacturer_string=Genius, product_string=Laser Mouse, usage_page=0, usage=0, interface_number=0]
[grigory@gr testRSA]$ 

and for fresh Windows XP it isn't work (only one windows i can find. I haven't Visual Studio for compile lib from source):

E:\testRSA\out\production\testRSA>java -cp ".;hidapi-1.1.jar" -Djava.library.pat
h="e:\testRSA\out\production\testRSA" Test
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.codeminders.hidap
i.HIDManager.init()V
        at com.codeminders.hidapi.HIDManager.init(Native Method)
        at com.codeminders.hidapi.HIDManager.<init>(HIDManager.java:53)
        at com.codeminders.hidapi.HIDManager.getInstance(HIDManager.java:121)
        at Test.main(Test.java:14)
Share:
17,781
bad_keypoints
Author by

bad_keypoints

There is no spoon - The Matrix (1999) "Do... or Do not. There is no Try" -Yoda Luke: I don't, I don't believe it. Yoda: That is why you fail. (in Star Wars: The Empire Strikes Back) Uh, well, we'll cross that bridge when we come to it (Battlefield 2: Bad Company) Python lover and learner. Computer vision enthusiast.

Updated on June 18, 2022

Comments

  • bad_keypoints
    bad_keypoints over 1 year

    Aren't there any system calls or OS specific functions that can be called by Java to get just the names of the USB devices attached?

    I've seen probably 6-7 questions here only, but everyone mentions the C++ functions GetRawInputDeviceList() etc, and they are not cross-platform compliant. Either for Windows in C# or C++ or for Linux only.

    But I'm working in Java. Also, this needs to be cross platform. Atleast, it needs to be working for Windows,Linux and Mac. I can work with terminal/shell/command-prompt commands also. I guess I can run them with Java.

    I've tried getFileSystemView and listRoots. But they give out names of all drives [dvd, hdd partitions,floppy etc].

    I need to get only USB devices.

    Please don't mention jUSB or JSR080. Why:

    jUSB: access to USB devices currently requires that they be connected to a GNU/Linux host system

    javax.usb: pre-alpha Windows implementation is not certified and requires a kernel driver.

    usb4java: basically, it just implements JSR80 with little more abstraction, perhaps

    Although to be honest I haven't tried libusb since it is in C++.

    If you are going to mention APIs, mention completely tested and tried ones, that work across Linux,Windows and Mac. If that wasn't the case, I wouldn't have put this question up. I've seen the mention of jUSB, javax.usb, etc on many other posts.