Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...