java 只是获取连接到系统的 USB 设备的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14018693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
just get the names of USB devices attached to a system?
提问by bad_keypoints
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?
没有任何系统调用或操作系统特定的函数可以被 Java 调用来获取所连接的 USB 设备的名称吗?
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.
我在这里只看到了大约 6-7 个问题,但每个人都提到了 C++ 函数GetRawInputDeviceList()
等,而且它们不跨平台兼容。适用于 C# 或 C++ 中的 Windows 或仅适用于 Linux。
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.
但我在 Java 工作。此外,这需要跨平台。至少,它需要适用于 Windows、Linux 和 Mac。我也可以使用终端/外壳/命令提示符命令。我想我可以用 Java 运行它们。
I've tried getFileSystemView
and listRoots
. But they give out names of all drives [dvd, hdd partitions,floppy etc].
我试过getFileSystemView
和listRoots
。但是他们给出了所有驱动器的名称 [dvd、hdd 分区、软盘等]。
I need to get only USB devices.
我只需要获取 USB 设备。
Please don't mention jUSB or JSR080. Why:
请不要提及 jUSB 或 JSR080。为什么:
jUSB:access to USB devices currently requires that they be connected to a GNU/Linux host system
jUSB:目前访问 USB 设备需要将它们连接到 GNU/Linux 主机系统
javax.usb:pre-alpha Windows implementation is not certified and requires a kernel driver.
javax.usb:pre-alpha Windows 实现未经认证,需要内核驱动程序。
usb4java:basically, it just implements JSR80 with little more abstraction, perhaps
usb4java:基本上,它只是实现了 JSR80,但可能没有更多的抽象
Although to be honest I haven't tried libusb since it is in C++.
虽然说实话我没有尝试过 libusb,因为它是在 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.
如果您要提及 API,请提及经过完全测试和试用的 API,它们适用于 Linux、Windows 和 Mac。如果不是这样,我就不会提出这个问题。我已经在许多其他帖子中看到提到 jUSB、javax.usb 等。
回答by Kazekage Gaara
You can use the jUsb
API, for Linux.
您可以将jUsb
API用于 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.
或者您可以terminal
使用Process
该类在 Linux 中启动,然后运行ls -la /dev/disk/by-id/usb-*
并捕获stdout
以了解结果。
For Windows, you can try this : How to find my USB flash drive's path with PowerShell
对于 Windows,你可以试试这个:How to find my USB flash drive's path with PowerShell
EDIT:
编辑:
For Windows, another helpful utility is the devcon.exe
.
对于 Windows,另一个有用的实用程序是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
编辑 2:对于 Mac,您可以启动terminal
usingProcess
类,然后运行system_profiler SPUSBDataType
回答by user1516873
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.
你可以试试javahidapi。我认为它是一些 c/c++ 代码和 JNI。声明 linux、mac 和 windows 支持。我已经尝试过使用 linux(好的),在虚拟框中使用干净的窗口(不好,UnsatisfiedLinkError,我认为错过了一些 MSVS 库)。如果你从源代码编译它,它应该可以工作,我相信。
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.
编辑输出显示只有一个插入 USB 设备,天才激光鼠标。
[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):
对于新的 Windows XP,它不起作用(我只能找到一个窗口。我没有用于从源代码编译库的 Visual Studio):
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)
回答by lenborje
Maybe things have improved since you first asked this question. I recently began exploring usb4javaon the Mac, and it seems to work. There is example code available, both for the low-level (libusb-like) API, and the high-level (javax) API.
也许自从你第一次问这个问题以来情况有所改善。我最近开始在 Mac 上探索usb4java,它似乎有效。有可用的示例代码,用于低级(类似 libusb)的 API 和高级 (javax) API。
To list all USB-devices, look the examples.
要列出所有 USB 设备,请查看示例。
I downloaded all libraries directly from usb4java.org and the examples from github. I did not manage to get the maven build working, but I could import the libraries and examples in Eclipse and run them all.
我直接从 usb4java.org 下载了所有库,并从 github 下载了示例。我没有设法让 maven 构建工作,但我可以在 Eclipse 中导入库和示例并运行它们。
There are same native code included in usb4java, but the library wraps them all quite beautifully and hides all the messy details, only extracting and deploying the native code at demand.
usb4java 中包含相同的本机代码,但该库将它们全部包装得非常漂亮并隐藏了所有杂乱的细节,仅在需要时提取和部署本机代码。