Compiling a Java USB program with unusual errors/w

2019-09-19 13:00发布

问题:

This question already has an answer here:

  • javax.usb.UsbException: Properties file javax.usb.properties not found 1 answer

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.

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();
                                          ^.

But the class file is created.

When I run this program, I get this exception:

Exception in thread "main" javax.usb.UsbException: Properties file
javax.usb.properties not found.

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.

回答1:

The properties file is usually part of the native package. I wasn't even aware of an implementation for OSX.

An example of how to set it for Linux is available at javax.usb FAQ.