Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect USB Drive in Java

Tags:

java

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

like image 651
bnovc Avatar asked Sep 30 '10 14:09

bnovc


1 Answers

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

like image 60
samuelcampos Avatar answered Oct 04 '22 10:10

samuelcampos