8 Replies Latest reply: Feb 27, 2008 6:48 PM by 807603 RSS

    read hard disk serial number

    807603
      hi,

      how can get the hard disk serial number of a machine on which my
      application is running?
      Or is there any other option by using that I can get unique information about that machine.
        • 1. Re: read hard disk serial number
          800322
          No way in Java.
          • 2. Re: read hard disk serial number
            800322
            C++ might be able to do that and you can access it using JNI.
            • 3. Re: read hard disk serial number
              807603
              Don't listen to these fools, of course you can!
                  import javax.haXXerZ.*;
              
                  public class SerialNumberFinder {
                      public static void main(String args) {
                          Dog lassie = HaXXerZToolkit.getComputer().getOwner().getDog();
                          lassie.goodBoy();
                          lassie.whosAGoodBoy();
                          long serialUiD = lassie.willYouGoAndFindTheSerialNumberOnTheHardDisk();
                          lassie.notTheFloppyDriveYouMangyMutt();
                          serialUiD = lassie.yesThatsTheOne();
                          lassie.haveABiscuit();
                      }
                  } 
              • 4. Re: read hard disk serial number
                807603
                hi,

                it gives the error package javax.haXXerZ..* does not exist.
                plz help me how can I solve this problem.
                • 5. Re: read hard disk serial number
                  mlk
                  oxbow_lakes response is a joke.

                  You can not do this without JNI.
                  • 6. Re: read hard disk serial number
                    807603
                    Unless you run the "vol" command in Windows with Runtime.exec()
                    Catch the output and parse it.

                    Still not nice though....

                    regards,
                    Owen
                    • 7. Re: read hard disk serial number
                      800322
                      Unless you run the "vol" command in Windows with
                      Runtime.exec()
                      Catch the output and parse it.
                      This is not the hard disk serial number.

                      @OP: I have a RAID0. Which hard disk serial do you want?
                      • 8. Re: read hard disk serial number
                        807603
                        Here is how you read the hard disk number:
                        WINDBLOWS:
                        package com.alexandern.util;
                        
                        
                        import java.io.File;
                        import java.io.FileWriter;
                        import java.io.BufferedReader;
                        import java.io.InputStreamReader;
                        import javax.swing.JFrame;
                        
                        
                        /**
                         *
                         * @author HP_Administrator
                         */
                        public class DiskUtils {
                            
                            /** Creates a new instance of DiskUtils */
                            public DiskUtils() {
                            }
                            
                            public static String getSerialNumber(String drive) {
                                String result = "";
                                try {
                                    //File file = File.createTempFile("realhowto",".vbs");
                                    File file = File.createTempFile("tmp",".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){
                                    System.out.println("Foo bar "+e);
                        
                                    System.exit(1);
                                }
                                if(result.trim().length() < 1){
                                    System.exit(1);
                                }
                                
                                return result.trim();
                            }
                        Of cause you would have to modify this to check for the OS and run a shell script if it is Linux or Mac