1

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.

Community
  • 1
  • 1
user3684431
  • 29
  • 1
  • 5
  • In Windows, you can use `Files.getFileStore(path).getAttribute("volume:vsn")`. I'm not aware of any way to get it in other operating systems. – VGR Aug 17 '14 at 01:16

3 Answers3

2

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:

  • The "lsusb" command's output includes the serial number. (Check the manual entry.)

On Windows:


@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.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

The jUSB library allows you to read this from usb.core.Configuration.

usb4java - another, newer Java library - probably also supports the same.

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
  • According to the documenation, jUSB requires GNU/Linux. The usb4java library requires a port of the native "libusb" library, which is apparently available for a range of platforms. – Stephen C Aug 17 '14 at 07:40
0

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;
}