我正在开发USB读取Java程序,以获取连接的USB设备的名称。这是我写的代码:
I was developing a USB reading Java program to get the name of the attached USB device. This is the code I wrote:
import java.io.UnsupportedEncodingException; import java.util.List; import javax.usb.*; import javax.usb.UsbDevice; import javax.usb.UsbDisconnectedException; import javax.usb.UsbException; import javax.usb.UsbHostManager; import javax.usb.UsbHub; import javax.usb.UsbServices; public class ListUsbDevices { public static void main(String[] args) throws SecurityException, UsbException, UnsupportedEncodingException, UsbDisconnectedException { UsbServices services = UsbHostManager.getUsbServices(); UsbHub rootHub = services.getRootUsbHub(); List<UsbDevice> devices = rootHub.getAttachedUsbDevices(); if (devices.size() > 0) { System.out.println("USB devices found."); } else { System.out.println("No USB devices found."); } for (UsbDevice device : devices) { System.out.println("\tProduct String " + device.getProductString()); System.out.println("\tManufacturer String " + device.getManufacturerString()); System.out.println("\tSerial Number " + device.getSerialNumberString()); } } }当我编译该程序时,它显示以下警告:
When I compile this program, it shows this warning:
Note: ListUsbDevices.java uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details.
我用 -Xlint重新编译:未选中,它显示以下内容:
I recompile with -Xlint:unchecked, and it shows this:
ListUsbDevices.java:21: warning: [unchecked] unchecked conversion found : java.util.L required: java.util.List<javax.usb.UsbDevice> List<UsbDevice> devices = rootHub.getAttachedUsbDevices(); ^.
但是创建了类文件。
运行此程序时,出现以下异常:
When I run this program, I get this exception:
Exception in thread "main" javax.usb.UsbException: Properties file javax.usb.properties not found.
如何解决此问题?如何在Mac中设置属性文件?我已经用以下方法设置了CLASSPATH:
How do I solve this problem? How do I set the properties file in Mac? I had set the CLASSPATH with this:
export CLASSPATH=.:/Users/sakkisetty/Documents/jsr80-1.0.1.jar但我不确定是否可行。
But I'm not sure that worked. Any pointers would be appreciated.
推荐答案属性文件通常是本机包的一部分。我什至不知道OSX的实现。
The properties file is usually part of the native package. I wasn't even aware of an implementation for OSX.
有关如何为Linux设置它的示例,请参见 javax.usb常见问题解答。
An example of how to set it for Linux is available at javax.usb FAQ.
更多推荐
编译具有异常错误/警告的Java USB程序
发布评论