1

I have this code, it works. This code can read the hard disk serial number. But this code found only if the SO is Windows. Now I want the same code for find serial number from Mac OSX SO.

It possibile?

This is the code:

public static String getSerialNumber(String drive) {
  String result = "";
    try {
      File file = File.createTempFile("realhowto",".vbs");
      file.deleteOnExit();
      FileWriter fw = new java.io.FileWriter(file);

      String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                  +"Set colDrives = objFSO.Drives\n"
                  +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
                  +"Wscript.Echo objDrive.SerialNumber";  // see note
      fw.write(vbs);
      fw.close();
      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
      BufferedReader input =
        new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = input.readLine()) != null) {
         result += line;
      }
      input.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return result.trim();
  }
bircastri
  • 2,169
  • 13
  • 50
  • 119
  • yes, you need to find the command line program which will do this on OSX, and then you can run it from Java. – Peter Lawrey Jan 11 '15 at 10:27
  • BTW this strips out newlines, which is generally not a good idea. i.e. the last word of one line will be joined with the first word of the next. It is generally better to copy the output to a StringWriter as it is. – Peter Lawrey Jan 11 '15 at 10:29
  • Some of the answers to this question may help: http://stackoverflow.com/q/2019244/2670892 – greg-449 Jan 11 '15 at 10:29
  • I have see the link, but the language that I used, is Java and no C – bircastri Jan 11 '15 at 12:06

0 Answers0