How can I get the serial number of the hard disk with Java?
-
2BTW - if your are doing this to prevent people stealing the software, you've 'already lost'. – Andrew Thompson Mar 30 '11 at 07:13
-
@thompson what do you mean of that comment? – developer Mar 30 '11 at 07:17
-
1he probably means that it is easy to fake the serial number to fool your code. – Stephen C Mar 30 '11 at 07:19
7 Answers
This serial number is created by the OS where formatting the drive and it's not the manufacturer serial number. It's unique, because it is created on the fly based on the current time information. AFAIK, there is no API that return that the manufacturer SN. At best, the SN of the HD firmware can be read but this will involve some very low-level API calls. Keep in mind that even if you get that number, there is no warranty that it will be unique since each manufacturer can assign the SN as they wish.
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class DiskUtils {
private DiskUtils() { }
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();
}
public static void main(String[] args){
String sn = DiskUtils.getSerialNumber("C");
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
null, sn, "Serial Number of C:",
javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
- 81,071
- 26
- 167
- 187
-
11. First the guy asked for hard disk serial number, not the partition one 2. Secondly, your code is not working properly as I got even negative results on my Win7 system + Java 7 (I even isolated the Visual Basic Script and executed and is still wrong) – Alex Dec 05 '12 at 13:25
-
1@Alex I think you are more genius than us.... you should know when someone accept the answer means it worked for him... Read the answer definition first and then downvote. Am I clear? – Pankaj Kumar Dec 05 '12 at 15:13
-
If my answer is wrong please explain it but do not make ironic comments. Beside I keep my previous opinion (as I re-read the whole stuff) and my downvote. – Alex Dec 05 '12 at 20:20
-
First of all, this code was written when I was using win-XP and java 5. I havn't checked with win7. I am not here for write code for everyone... hope you understand – Pankaj Kumar Dec 06 '12 at 05:14
-
And this is not for my upvote getting back from you. You are free to express yourself here. – Pankaj Kumar Dec 06 '12 at 05:31
-
`return StringUtils.leftPad(Integer.toHexString(Integer.parseInt(result.trim())).toUpperCase(), 8, "0")` using [org.apache.commons.lang](http://www.docjar.com/html/api/org/apache/commons/lang/StringUtils.java.html) package will result in HEX representation of partition SN. – SYNCRo Sep 13 '14 at 02:39
-
Good news there is a volume:vsn attribute for the FileStore object.
for (FileStore store: FileSystems.getDefault().getFileStores()) {
System.out.format("%-20s vsn:%s\n", store, store.getAttribute("volume:vsn"));
}
The output looks like an int (e.g. -1037833820, 194154)
Bad news it is windows specific for testing purposes as they say in WindowsFileStore jdk8u source. Might work in other platforms and in the future, or not.
Other options (JNI, executing and parsing CLI) are also system dependent. Are more ellaborate but might not suddenly break up when you change the runtime. But I think the attribute solution is much simpler and I am totally going with it until I have something better.
- 668
- 5
- 16
Java runs on a virtual machine which doesn't have hard drives only files and filesystems. You should be able to get this information by running the approriate command line utility from Java.
One Linux you can do
hdparm -i /dev/hda
- 513,304
- 74
- 731
- 1,106
If you are using a Windows OS which is Win7 SP1 or higher, you can simply get it by executing the following cmd command in your java program, via wmic.
wmic diskdrive get serialnumber
If this command returns "Invalid XML content." you may wanna apply the hotfix file as described in here.
- 223
- 1
- 3
- 14
on windows machine you can use
public static String getSerialKey(Character letter) throws Exception{
String line = null;
String serial = null;
Process process = Runtime.getRuntime().exec("cmd /c vol "+letter+":");
BufferedReader in = new BufferedReader(
new InputStreamReader(process.getInputStream()) );
while ((line = in.readLine()) != null) {
if(line.toLowerCase().contains("serial number")){
String[] strings = line.split(" ");
serial = strings[strings.length-1];
}
}
in.close();
return serial;
}
- 3,905
- 1
- 16
- 18
I would imagine you'd have to implement that feature in C or C++ and use JNI to access it from Java.
- 118,129
- 19
- 204
- 237