how te get volume serial number in java ?

I need to get the volume serial number using java. Code example will be very useful.

 Objective is to use this information to lock a particular java program for a particular PC only.



Sabbir456Asked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
Well, after the try/catch

you'll need a extra instruction

   return "?";

to make it compile

;°)
0
 
zzynxSoftware engineerCommented:
>> Objective is to use this information to lock a particular java program for a particular PC only.

One common way to uniquely (or as close to uniquely as possible) identify a computer using Java,
is to get the MAC address of the network adapter. That's usually sufficient.
If you're using Java 6 then you can use the NetworkInterface class for that:
http://java.sun.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress()

If you're pre-Java 6 then you might try the code here:
http://thecodersblog.blogspot.com/2007/06/java-getting-mac-addresses-of-network.html
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Sabbir456Author Commented:
hi zzynx,
Yes we are using java 6 and was thinking like wise to use MAC Address .. but the target customers (schools) of the solution mostly have single PC (without any network interface), that's why we are looking for an alternative like HDD serial or any other unique system indentifier. thanks.

0
 
zzynxSoftware engineerCommented:
Right.
Well here's the code you asked for:


import java.io.*;
 
public class GetVolumeSerialNumberDemo {
    
    public static void main(String args[]) {
        System.out.println("Volume Serial ID = <" + getVolumeSerialId() + ">");
    }
    
    private static String getVolumeSerialId() {
        
        try {
            Process p = Runtime.getRuntime().exec("cmd /C dir");
            BufferedReader in = new BufferedReader(
                                new InputStreamReader(p.getInputStream()));
            String line = null;
            int count = 0;
            while ((line = in.readLine()) != null) {
                count++;
                if (count<2)
                    continue;
                if (count>2)
                    break;
                return line.substring( line.lastIndexOf(' ')+1 );
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window

1
 
zzynxSoftware engineerCommented:
This gives me as output:
Volume Serial ID = <F4F2-EFC8>

Open in new window

0
 
Sabbir456Author Commented:
Hi, Thanks a lot. Solution worked just fine, Though we have found that volume serial number can be changed and user can cnange the volume serial to run unliscensed copy. We will actually need the manufacturer serial no of HDD, but may be that is not possible only with java ..
0
 
zzynxSoftware engineerCommented:
thanx 4 axxepting

>> We will actually need the manufacturer serial no of HDD
Then you'll probably need to use some 3rd party library
0
All Courses

From novice to tech pro — start learning today.