I am trying to use java to detect when a USB is inserted and to read from it. I am currently using javax.usb to try to accomplish this however I get this error (line 11) when running my program

javax.usb.UsbException: The property javax.usb.services is not defined as the implementation class of UsbServices

I have inserted the javax.usb.properties file in the top directory of the jar (which seemed to solve my previous error)

Here is my code

import java.util.Iterator;
import java.util.List;

import javax.usb.*;

public class Main {


    public static void main(String[] args) {
        try {

            UsbServices serv = UsbHostManager.getUsbServices();
            UsbHub root = serv.getRootUsbHub();

            List devices = root.getAttachedUsbDevices();
            Iterator itr = devices.iterator();
            while (itr.hasNext()){
                UsbDevice dev = (UsbDevice) itr.next();
                if (dev.isUsbHub()){
                    System.out.println("Is USB Hub");
                }
            }

        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UsbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

Thank you for your help.

Recommended Answers

All 4 Replies

don't really think Java is the optimal choice here

I know that, but I am not experienced enough in c based languages to work with them yet.

maybe this might help

Thanks. I looked at that website already, but thanks for linking it anyway. I think I was able to find a way to work around it by using the File.listRoots() method to get the base roots, and then re-checking every couple seconds to see if it changed. If so then the usb was inserted. Then I use that directory to read the files I am looking for. In your opinion would this work? I know it isn't the most efficient way, but it seems to be the easiest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.