title: java getting hard disk serial
body: <p>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 <code>hdparm -I /dev/sda | grep Serial</code> in the terminal It shows the serial number of the hard drive.</p>
  
  <p>Question how can I get or display the serial number. </p>
  
  <p>here's my code:</p>
  
  <pre><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") &gt;= 0);
      }
      private static boolean isUnix()
      {
          return (OS.indexOf("nux") &gt;= 0);
      }
  
      public static void main(String args[])
      {
          MainClass f=new MainClass();
          f.getVol("C");
          System.out.println(serial);
      }
  </code></pre>