Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java getting hard disk serial

Tags:

java

linux

I'm trying to make a java program which will get the serial of the hard disk thru cmd or terminal. I'm trying it on both OS (Windows, Linux). I'm having trouble with Linux, it returns a whitespace, when I type hdparm -I /dev/sda | grep Serial in the terminal It shows the serial number of the hard drive.

Question how can I get or display the serial number.

here's my code:

    private static String OS= System.getProperty("os.name").toLowerCase();
    private static String system;
    private static String serial;
    private void getVol(String drive)
    {
        String query=new String();
        if(isWindows())
        {
            query="cmd /c"+" vol "+drive+":";
        }
        else if(isUnix())
        {
            query="hdparm -I /dev/sda | grep Serial";
        }
        try
        {
            Runtime rt=Runtime.getRuntime();
            InputStream is=rt.exec(query).getInputStream();
            InputStreamReader isr=new InputStreamReader(is);
            BufferedReader br=new BufferedReader(isr);
            String line;
            if(isWindows())
            {
                br.readLine();
                line=br.readLine();
                line=line.substring(line.lastIndexOf(" ")+1);
                serial=line;
            }
            else if(isUnix())
            {
                line=br.readLine();
                serial=line;
            }
        }catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }

    private static boolean isWindows() {
        return (OS.indexOf("win") >= 0);
    }
    private static boolean isUnix()
    {
        return (OS.indexOf("nux") >= 0);
    }

    public static void main(String args[])
    {
        MainClass f=new MainClass();
        f.getVol("C");
        System.out.println(serial);
    }
like image 420
askManiac Avatar asked Mar 23 '26 18:03

askManiac


1 Answers

It's being a while since this was posted, but @giusc solution didn't work for me, so posting my research for others.

Get Hard Disk Drive Serial Number on Linux:

public static void main(String[] args) throws Exception {

    String sc = "/sbin/udevadm info --query=property --name=sda"; // get HDD parameters as non root user
    String[] scargs = {"/bin/sh", "-c", sc};

    Process p = Runtime.getRuntime().exec(scargs);
    p.waitFor();

    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 
    String line;
    StringBuilder sb  = new StringBuilder();

    while ((line = reader.readLine()) != null) {
        if (line.indexOf("ID_SERIAL_SHORT") != -1) { // look for ID_SERIAL_SHORT or ID_SERIAL
            sb.append(line);
        }    
    }

    System.out.println("HDD Serial number:" + sb.toString().substring(sb.toString().indexOf("=") + 1));
}

Get Hard Disk Drive Serial Number (Manufacture) Windows:

public static void main(String[] args) {
    String sc = "cmd /c" + "wmic diskdrive get serialnumber";

    Process p = Runtime.getRuntime().exec(sc);
    p.waitFor();

    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String line;
    StringBuilder sb = new StringBuilder();

    while ((line = reader.readLine()) != null) {
        sb.append(line);
    } 

    System.out.println("HDD Serial number: " + sb.substring(sb.toString().lastIndexOf("r") + 1).trim());
}
like image 92
Sasha Avatar answered Mar 26 '26 09:03

Sasha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!