samuelcampos / usbdrivedetector Goto Github PK
View Code? Open in Web Editor NEWA Java library to get a list of all usb storage devices connected to the computer.
License: MIT License
A Java library to get a list of all usb storage devices connected to the computer.
License: MIT License
Does your Library has a callback method that informs the Java Application a new USB inserted?
Or it has to be done manually using some of thread?
Can your Library be adapted for other drives such C: or D:?
I'm running your lib on
Linux 4.14.98 aarch64
I'm getting this on, what I assume to be, every listener interval
2020-10-14 16:09:48.483 WARN 2774 --- [ Thread-11] n.s.u.process.CommandExecutor : Abnormal command 'df -l' terminantion. Exit value: 1
This is the DF usage as printed by the OS. As you can see, there is no -l switch available.
Usage: df [-PkmhT] [FILESYSTEM]...
usbdrivedetector library is not detecting the USB drive(FAT32) in the MAC OS (Monterey) with the latest JRE update 1.8.0_341.
In fact Library sends the connected event but driveDetector.getRemovableDevices() returns empty value. I even tried to run the Application with sudo mode and still same issue persists. Can you please help me on this.
Failure logs:
[Thread-14] ERROR net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager - An IUSBDriveListener threw an exception
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager.sendEventToListeners(USBDeviceDetectorManager.java:221)
at net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager.lambda$updateConnectedDevices$0(USBDeviceDetectorManager.java:202)
at java.util.ArrayList.forEach(ArrayList.java:1259)
at net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager.updateConnectedDevices(USBDeviceDetectorManager.java:201)
at net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager.access$100(USBDeviceDetectorManager.java:32)
at net.samuelcampos.usbdrivedetector.USBDeviceDetectorManager$ListenerTask.run(USBDeviceDetectorManager.java:247)\
Actual occurs an IllegalArgumentException when a connected USB Drive is removed, to resolved this i added the "if" code as follow :
if (root != null && root.isDirectory())
{
USBStorageDevice device = new USBStorageDevice(root);
listDevices.add(device);
}
in the addUSBDevice method of the AbstractStorageDeviceDetector.
if (pollingInterval >= 0) {
throw new IllegalArgumentException("'pollingInterval' must be greater than 0");
}
Since this is a library you should only depend on a logging API and not its implementation.
This will allow users of the library to choose their logging implementation (for instance I prefer logback).
More specifically, you should depend on org.slf4j:slf4j-api in the maven compile scope (default scope) and NOT org.slf4j:slf4j-simple.
In the test runtime scope you can depend on org.slf4j:slf4j-simple if you want so that you see the logs in your tests.
it updates state of device after 5-6 secs delay .
Hi,
When I'm trying to do it on windows 10 I'm getting:
"no instances available" when trying it from CMD.
Any idea?
Hi
Will this open source provided by you detect an external hard disk?
Thanks,
Sashi
Hello
in the Function addUSBDevice is a try catch block missing
try {
USBStorageDevice device = new USBStorageDevice(root);
listDevices.add(device);
} catch (IllegalArgumentException e) {
logger.debug("Could not add Device - IllegalArgumentException: "
+ e.getMessage());
}
Can you please update the maven repo with the latest version? It looks like the latest version on maven central is 2.1.1
I need to use verify the pendrive by its vendor ID and Product ID for its uniqueness so is there way to get VID and PID?
I get lots of errors when I check out master (53 to be exact).
e.g.
log cannot be resolved AbstractStorageDeviceDetector.java /usbdrivedetector/src/main/java/net/samuelcampos/usbdrivedetector/detectors line 83 Java Problem
there are lots of these on log lines.
also other errors.
This is a feature request for when the Library runs on Linux.
It would be nice if this library would detect new USB device that are connected but not mounted and automatically mount them if possible.
I think this should not be enabled by default, and you would have to specify a directory where to mount new USBs. Also, I assume only programs that run as root could potentially do that.
It would be possible to detect USB that are connected but not mounted by keeping track of added/removed devices in /dev
AbstractStorageDeviceDetector.getStorageDevicesDevices()
Seems like a failed copy/paste to me.
This library appears to leak native memory under Windows 10.
After a night of running USBDriveDetector, with a polling interval of 500ms, my Windows machine has ran totally out of memory. Heap of the Java application has not increased at all, so it must be native memory that exhausts the system.
Unmounting devices doesn't work for Windows 10 and Java 11.
Steps to reproduce:
import java.io.IOException;
import java.nio.file.FileSystems;
import net.samuelcampos.usbdrivedetector.detectors.AbstractStorageDeviceDetector;
import net.samuelcampos.usbdrivedetector.unmounters.AbstractStorageDeviceUnmounter;
public class Test {
public static void main(String[] args) throws IOException {
FileSystems
.getDefault()
.getRootDirectories()
.forEach(path -> System.out.println(path.toString()));
var deviceDetector = AbstractStorageDeviceDetector.getInstance();
var driveUnmounter = AbstractStorageDeviceUnmounter.getInstance();
var usb = deviceDetector
.getStorageDevices()
.get(0); // I have 1 usb flash connected.
driveUnmounter.unmount(usb);
FileSystems
.getDefault()
.getRootDirectories()
.forEach(path -> System.out.println(path.toString()));
}
}
C:\
D:\
E:\
TRACE [net.samuelcampos.usbdrivedetector.process.CommandExecutor] [main] - Running command: C:\WINDOWS\System32\wbem\wmic.exe logicaldisk where drivetype=2 get DeviceID,VolumeSerialNumber,VolumeName
TRACE [net.samuelcampos.usbdrivedetector.detectors.AbstractStorageDeviceDetector] [main] - Device found: E:\
C:\
D:\
E:\
it's expected that root E will be unmounted and second .getRootDirectories() will not list it. Instead, E is still present, connected and is accessable by OS and/or application.
conclusion - AbstractStorageDeviceUnmounter.unmount is not working at all, at least for Windows 10 + Java 11.
I am running Windows 7
(yea I know...its a bummer but I have to wait a bit longer). My NTFS
hard disk DriveType 3
is not picked up. We use USB external hard disk to transfer stuff to this Windows machine. However while doing some testing the api picks up USB memory sticks DriveType 2
just fine. Our NTFS
formatted USB hard disk DriveType 3
however is not picked up at all.
Running Linux
, DriveType 3
is found. Is Windows (7 and maybe also newer?) DriveType 3
just not supported?
I have mentioned this problem here also.
Originally posted by @Avec112 in #37 (comment)
Hi there.
Could you implement a getDevice() in USBStorageDevice (for ex: /dev/sdc1) ? I need it because I'm having some problems while umount devices with "space" in its name, such as "Ubuntu 18.04 Install".
The file system shouldn't mater. It should work with FAT32
It is not detecting. I have an external hard disk with NTFS partition. I created one FAT32 partition. When I plugin the hard disk and run the Java program, it does not show the external hard disk.
Originally posted by @sashi2k in #36 (comment)
I use code:
driveDetector.getRemovableDevices().forEach(System.out::println);
driveDetector.addDriveListener (System.out :: System.out.println)
It just writes the USB drives connected to the computer and the program stops.
I try to set the poolin interval but without success.
How to permanently monitor the connection of a new USB flash?
Windows 10
Thanks
Hello,
I would be very interested in including your library for a work project but I could not find any mentioning of a licence. Could I use you licence in a commercial product ?
Would you release your project with a licence such as the MIT Licence (https://choosealicense.com/licenses/mit/) ?
Thanks,
Gwendal
The polling thread should be a daemon thread.
This way, if some code add a listener but does not remove it, the JVM is not left indefinitely running.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.