Reputation: 167
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 hdparm -I /dev/sda | grep Serial
in the terminal It shows the serial number of the hard drive.
Question how can I get or display the serial number.
here's my 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") >= 0);
}
private static boolean isUnix()
{
return (OS.indexOf("nux") >= 0);
}
public static void main(String args[])
{
MainClass f=new MainClass();
f.getVol("C");
System.out.println(serial);
}
Upvotes: 3
Views: 2992
Reputation: 1570
It's being a while since this was posted, but @giusc solution didn't work for me, so posting my research for others.
Get Hard Disk Drive Serial Number on Linux:
public static void main(String[] args) throws Exception {
String sc = "/sbin/udevadm info --query=property --name=sda"; // get HDD parameters as non root user
String[] scargs = {"/bin/sh", "-c", sc};
Process p = Runtime.getRuntime().exec(scargs);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
if (line.indexOf("ID_SERIAL_SHORT") != -1) { // look for ID_SERIAL_SHORT or ID_SERIAL
sb.append(line);
}
}
System.out.println("HDD Serial number:" + sb.toString().substring(sb.toString().indexOf("=") + 1));
}
Get Hard Disk Drive Serial Number (Manufacture) Windows:
public static void main(String[] args) {
String sc = "cmd /c" + "wmic diskdrive get serialnumber";
Process p = Runtime.getRuntime().exec(sc);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
}
System.out.println("HDD Serial number: " + sb.substring(sb.toString().lastIndexOf("r") + 1).trim());
}
Upvotes: 2
Reputation:
Your problem is almost certainly that running hdparm
requires root privileges. It can't be run by a normal user.
Additionally, the "serial number" that you're reading on Windows is the volume serial number of the primary file system, not of the hard drive. It can be changed rather easily (e.g, using VolumeID).
Upvotes: 0