2014-09-01 24 views
0

我谷歌有关获得连接到Linux操作系统的USB信息,并发现USB4JAVA是非常有用的做同样的。我发现如下代码示例。现在运行后,我得到了“java.lang.ExceptionInInitializerError”的异常。我用使用“USB4JAVA.jar”获取有关在Linux中连接的设备的信息

代码:

import org.usb4java.ConfigDescriptor; 
import org.usb4java.Context; 
import org.usb4java.DescriptorUtils; 
import org.usb4java.Device; 
import org.usb4java.DeviceDescriptor; 
import org.usb4java.DeviceHandle; 
import org.usb4java.DeviceList; 
import org.usb4java.LibUsb; 
import org.usb4java.LibUsbException; 
/** 
* Dumps the descriptors of all available devices. 
* 
* @author Klaus Reimer <[email protected]> 
*/ 
public class DumpDevices 
{ 
    /** 
    * Dumps all configuration descriptors of the specified device. Because 
    * libusb descriptors are connected to each other (Configuration descriptor 
    * references interface descriptors which reference endpoint descriptors) 
    * dumping a configuration descriptor also dumps all interface and endpoint 
    * descriptors in this configuration. 
    * 
    * @param device 
    * The USB device. 
    * @param numConfigurations 
    * The number of configurations to dump (Read from the device 
    * descriptor) 
    */ 
    public static void dumpConfigurationDescriptors(final Device device, 
    final int numConfigurations) 
    { 
     for (byte i = 0; i < numConfigurations; i += 1) 
     { 
      final ConfigDescriptor descriptor = new ConfigDescriptor(); 
      final int result = LibUsb.getConfigDescriptor(device, i, descriptor); 
      if (result < 0) 
      { 
       throw new LibUsbException("Unable to read config descriptor", 
       result); 
      } 
      try 
      { 
       System.out.println(descriptor.dump().replaceAll("(?m)^", 
       " ")); 
      } 
      finally 
      { 
       // Ensure that the config descriptor is freed 
       LibUsb.freeConfigDescriptor(descriptor); 
      } 
     } 
    } 
    /** 
    * Dumps the specified device to stdout. 
    * 
    * @param device 
    * The device to dump. 
    */ 
    public static void dumpDevice(final Device device) 
    { 
     // Dump device address and bus number 
     final int address = LibUsb.getDeviceAddress(device); 
     final int busNumber = LibUsb.getBusNumber(device); 
     System.out.println(String 
     .format("Device %03d/%03d", busNumber, address)); 
     // Dump port number if available 
     final int portNumber = LibUsb.getPortNumber(device); 
     if (portNumber != 0) 
      System.out.println("Connected to port: " + portNumber); 
     // Dump parent device if available 
     final Device parent = LibUsb.getParent(device); 
     if (parent != null) 
     { 
      final int parentAddress = LibUsb.getDeviceAddress(parent); 
      final int parentBusNumber = LibUsb.getBusNumber(parent); 
      System.out.println(String.format("Parent: %03d/%03d", 
      parentBusNumber, parentAddress)); 
     } 
     // Dump the device speed 
     System.out.println("Speed: " 
     + DescriptorUtils.getSpeedName(LibUsb.getDeviceSpeed(device))); 
     // Read the device descriptor 
     final DeviceDescriptor descriptor = new DeviceDescriptor(); 
     int result = LibUsb.getDeviceDescriptor(device, descriptor); 
     if (result < 0) 
     { 
      throw new LibUsbException("Unable to read device descriptor", 
      result); 
     } 
     // Try to open the device. This may fail because user has no 
     // permission to communicate with the device. This is not 
     // important for the dumps, we are just not able to resolve string 
     // descriptor numbers to strings in the descriptor dumps. 
     DeviceHandle handle = new DeviceHandle(); 
     result = LibUsb.open(device, handle); 
     if (result < 0) 
     { 
      System.out.println(String.format("Unable to open device: %s. " 
      + "Continuing without device handle.", 
      LibUsb.strError(result))); 
      handle = null; 
     } 
     // Dump the device descriptor 
     System.out.print(descriptor.dump(handle)); 
     // Dump all configuration descriptors 
     dumpConfigurationDescriptors(device, descriptor.bNumConfigurations()); 
     // Close the device if it was opened 
     if (handle != null) 
     { 
      LibUsb.close(handle); 
     } 
    } 
    /** 
    * Main method. 
    * 
    * @param args 
    * Command-line arguments (Ignored) 
    */ 
    public static void main(final String[] args) 
    { 
     // Create the libusb context 
     final Context context = new Context(); 
     // Initialize the libusb context 
     int result = LibUsb.init(context); 
     if (result < 0) 
     { 
      throw new LibUsbException("Unable to initialize libusb", result); 
     } 
     // Read the USB device list 
     final DeviceList list = new DeviceList(); 
     result = LibUsb.getDeviceList(context, list); 
     if (result < 0) 
     { 
      throw new LibUsbException("Unable to get device list", result); 
     } 
     try 
     { 
      // Iterate over all devices and dump them 
      for (Device device: list) 
      { 
       dumpDevice(device); 
      } 
     } 
     finally 
     { 
      // Ensure the allocated device list is freed 
      LibUsb.freeDeviceList(list, true); 
     } 
     // Deinitialize the libusb context 
     LibUsb.exit(context); 
    } 
} 

例外,我得到:

Exception in thread "main" java.lang.ExceptionInInitializerError 
    at find_usb.DumpDevices.main(DumpDevices.java:132) 
Caused by: org.usb4java.LoaderException: Native library not found in classpath: /org/usb4java/linux-x86/libusb4java.so 
    at org.usb4java.Loader.extractLibrary(Loader.java:281) 
    at org.usb4java.Loader.load(Loader.java:358) 
    at org.usb4java.LibUsb.<clinit>(LibUsb.java:640) 
+0

看起来很像它在classpath中找不到本地库。 – 2014-09-01 10:49:53

+0

所以我必须做什么? – user2996582 2014-09-01 11:05:09

+0

我也面临同样的问题..如果你解决了这个问题..请发布你的答案她.. @ user2996582 – 2014-10-27 16:51:37

回答

0

说明在异常状态

Native library not found in classpath: /org/usb4java/linux-x86/libusb4java.so 

什么arch是你的JVM的系统?

如果您运行的是x64,则x86库不起作用,您需要将本机库设置为正确的x64。

但是,如果您正在运行x86 JVM,请检查文件是否真的存在以及它是否正确。当你解开它时,可能出现了问题?或者它在另一个文件夹中?

+0

我是新的java。我只在我的netbeans项目库中添加usb4java.jar。我不知道如何以及在何处或找到“classpath:/org/usb4java/linux-x86/libusb4java.so” – user2996582 2014-09-01 11:03:42

+1

@Korashen注:库依赖于JVM拱,而不是OS拱。如果您的x64系统具有x86 JVM,则必须使用x86库。操作系统拱不重要。 – BackSlash 2014-09-01 11:07:11

+0

我有Linux操作系统(回溯)和x86 JVM与x86系统 – user2996582 2014-09-01 11:07:20

0

看起来你忘了在项目中添加libusb4java-1.2.0-linux-x86.jar库。

相关问题