How can I retrieve the manufacturer serial number of an USB flash drive in java?
i look at this
How to get manufacturer serial number of an USB flash drive?
but it doesn't help me.
How can I retrieve the manufacturer serial number of an USB flash drive in java?
i look at this
How to get manufacturer serial number of an USB flash drive?
but it doesn't help me.
I don't think there is a pure Java solution for this that works across multiple platforms.
You will probably need to resort to "scraping" the output of an external command. (Or figure out what they are doing, and replicate it in a native code library ...)
On Linux:
On Windows:
Third-party commands called "usp64" and "USBDeview" do this: see http://ashfaqshinwary.wordpress.com/tag/how-to-find-usb-serial-number-from-windows/.
Apparently, you can use wmic
to get the serial number, though you may need to apply a Microsoft fix to make it work: https://superuser.com/questions/600394/get-usb-key-manufacturer-serial-number
@FoggyDay has found a couple of 3rd-party libraries that supposedly support this:
jUSB is apparently only available for GNU/Linux.
usb4java supports a wider range of platforms.
Both depends on platform specific native libraries.
The comment that suggests using Files.getFileStore(path).getAttribute("volume:vsn")
is incorrect, I think. The Java source code implies that that is returning a volume serial number that is assigned by Windows, not the manufacturer-provided serial number. The relevant code is here.
The jUSB library allows you to read this from usb.core.Configuration.
usb4java - another, newer Java library - probably also supports the same.
this method help you to read USB's sereal number
public static String getSerialNumber() {
String command = "powershell.exe Get-WmiObject Win32_DiskDrive | Select-Object SerialNumber\n";
String serialNumber = null;
try {
Process powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();
BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
String line;
while ((line = stdout.readLine()) != null) {
System.out.println("PowerShell output: " + line);
if (!line.trim().isEmpty()) {
serialNumber = line.trim();
}
}
stdout.close();
BufferedReader stderr = new BufferedReader(new InputStreamReader(powerShellProcess.getErrorStream()));
while ((line = stderr.readLine()) != null) {
System.out.println(line);
}
stderr.close();
} catch (IOException e) {
e.printStackTrace();
}
return serialNumber;
}