javax.usb.UsbException: Properties file javax.usb.

2019-01-20 04:21发布

问题:

I used the following code to get the manufacturerCode of the usb device attached to the system. I added the jsr80-1.0.1 jar. And I got the following error javax.usb.UsbException:

Properties file javax.usb.properties not found.

Any suggestions?

import java.io.UnsupportedEncodingException;
    import java.util.*;
    import javax.usb.*;

    public class USBListener {
        public static void main(String[] args) {
            try{
                UsbServices services = UsbHostManager.getUsbServices();
                UsbHub root = services.getRootUsbHub();
                listDevices(root);
            } catch (Exception e) {
                System.out.println(e);
            }
        }

        public static void listDevices(UsbHub hub) throws UnsupportedEncodingException, UsbException {
            List devices = hub.getAttachedUsbDevices();
            Iterator iterator = devices.iterator();
            while(iterator.hasNext()) {
                UsbDevice device = (UsbDevice)iterator.next();
                describe(device);
                if(device.isUsbHub()) {
                    System.out.println("is hub");
                }
            }
        }

        public static void describe(UsbDevice device) 
            throws UnsupportedEncodingException, UsbException {
            UsbDeviceDescriptor descriptor = device.getUsbDeviceDescriptor();
            byte manufacturerCode = descriptor.iManufacturer();
            System.out.println("Manufacturer index: " + manufacturerCode);
            System.out.println("Manufacturer String: " + device.getString(manufacturerCode));
            System.out.println("USB version: " + decodeBCD(descriptor.bcdUSB()));
            System.out.println("Maximum control packet size: " + descriptor.bMaxPacketSize0());

        }

        public static String decodeBCD(short bcd) {
            int upper = (0xFF00 & bcd) >> 8;
            int middle = (0xF0 & bcd) >> 4;
            int lower = 0x0F & bcd;
            return upper + "." + middle + "." + lower;
        }
    }

回答1:

You need this file on your classpath. From the docs:

The javax.usb.properties file is a Java properties file that is required by the API implementation loader class. The properties file must be loadable by normal means (i.e. it must be in the CLASSPATH) and it must contain the property javax.usb.services. This property must be defined. Its value must be the fully qualified class name of a class that implements the interface javax.usb.UsbServices. This class will be loaded as the implementation of javax.usb.

And further, if you are seeing this error, you presumably haven't got a javax.usb implementation:

You need a javax.usb implementation; the file is provided by all javax.usb implementations

See here: http://javax-usb.sourceforge.net/faq.html#what_is_properties_file



标签: java usb