How to get motherboard serial : JAVA

This is more like an extension to the my last post. The previous entry was regarding the extraction of S/N using VB script but this is for the java lovers. So here you go:

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MiscUtils {
private MiscUtils() { }

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

String vbs =
"Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n"
+ "Next \n";

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();
}

Advertisements

8 thoughts on “How to get motherboard serial : JAVA

    • What did u actually do? Could u explain that step by step so it would be better for me a give you and informed response. By the way, you would require windows script installed in the machine.

  1. There is any chances to get motherboard serial number in java no mater user S.O.?
    Because I write an application that should be working on windows, linux and mac and I really need the motherboard serial number.

    Thank you very much in advance.

  2. It does not work for me to.. gives following error as,
    java.io.IOException: Cannot run program “cscript”: CreateProcess error=5, Access is denied

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s