title: How todetect usb vendor id and product id using java
body: <p>I am writing an java application that detects usb device.When i executes the program it is showing all the usb devices in pc actually but i want the connected usb device only this is my java code:</p>
  
  <pre><code>package com.lakshman.sundeep;
  
  import org.usb4java.Context;
  import org.usb4java.Device;
  import org.usb4java.DeviceDescriptor;
  import org.usb4java.DeviceList;
  import org.usb4java.LibUsb;
  import org.usb4java.LibUsbException;
  
  public class UTMusb 
  {
      public static int vid = 6790;
      public static int pid = 29987;
  
      public static void main(String[] args) 
      {
          Context context = new Context();
          int result = LibUsb.init(context);
          if(result != LibUsb.SUCCESS)
          {
              throw new LibUsbException("Unable to initialize the usb device",result);
          }
          DeviceList list = new DeviceList();
          result = LibUsb.getDeviceList(null, list);
          if(result &lt; 0 )throw new LibUsbException("Unable to get device list",result);
              try
              {
                  for(Device device : list)
                  {
                      DeviceDescriptor device_descriptor = new DeviceDescriptor();
                      result = LibUsb.getDeviceDescriptor(device, device_descriptor);
                      if(result != LibUsb.SUCCESS)throw new LibUsbException("Unable to get device descriptor : ",result);
                      System.out.println("Product id is : "+device_descriptor.idProduct()+" "+"Vendor id is : "+device_descriptor.idVendor());
                      if(device_descriptor.idProduct()==pid &amp;&amp; device_descriptor.idVendor()==vid)
                      {
                          System.out.println("Product id and vendor id was matched");
                      }
                      else
                      {
  
                          System.out.println("Product id and vendor id was not matched");
                      }
  
                  }
  
              }
              finally
              {
                  LibUsb.freeDeviceList(list, true);
              }
  
  
      }
  
  }
  </code></pre>
  
  <p>this is my output:</p>
  
  <pre><code>Product id is : -29658 Vendor id is : -32634
  Product id and vendor id was not matched
  Product id is : -29651 Vendor id is : -32634
  Product id and vendor id was not matched
  Product id is : -16294 Vendor id is : 1133
  Product id and vendor id was not matched
  Product id is : -15587 Vendor id is : 1133
  Product id and vendor id was not matched
  Product id is : 29987 Vendor id is : 6790
  Product id and vendor id was matched
  Product id is : -32768 Vendor id is : -32633
  Product id and vendor id was not matched
  Product id is : -32760 Vendor id is : -32633
  Product id and vendor id was not matched
  Product id is : -29647 Vendor id is : -32634
  Product id and vendor id was not matched
  </code></pre>