How can I detect when a USB drive is attached to a computer in Windows, Linux, or Mac?
The only way I have seen online to do this is to iterate the drives, but I don't think there is a very good way to do that cross-platform (e.g. File.listRoots() in Linux only returns "/"). Even in Windows this would cause problems reading from every device, such as a network drive that takes a long time to access.
There is a library called jUsb that sounds like it accomplishes this in Linux, but it doesn't work in Windows. There is also an extension to this called jUsb for Windows, but that requires users to install a dll file and run a .reg. Neither of these seem to be developed for several years, so I'm hoping a better solution exists now. They're also overkill for what I need, when I only want to detect if a device is connected that contains a file I need.
[Edit] Furthermore, jUsb apparently doesn't work with any recent version of Java, so this isn't even an option...
Thanks
I've made a small library to detect USB storage devices on Java. It works on Windows, OSX and Linux. Take a look at: https://github.com/samuelcampos/usbdrivedetector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With