Get Hardware information in Java application

View more Tutorials:

Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- Java API to retrieve computer hardware information?

Sometimes you need to use Java to get information of computer hardware, including Serial Number of the mainboard, serial of hard drive, CPU, ...

Unfortunately, Java has no such API, or may be there, but it was not released for free for programmers. However, on Windows you can obtain this information by executing the VB script.

2- Get hardware info using vbscript file

OK, in order to be more simple, you can create a file named myscript.vbs:

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select Name,UUID,Vendor,Version from Win32_ComputerSystemProduct")

For Each objItem in colItems  

    Wscript.Echo objItem.Name
    Wscript.Echo objItem.UUID
    Wscript.Echo objItem.Vendor
    Wscript.Echo objItem.Version
On windows click file to run it:
In which Win32_ComputerSystemProduct  is a class of Visual Basic, the properties of this class:
string Caption
string Description
string IdentifyingNumber
string Name
string SKUNumber
string UUID
string Vendor
string Version
You can run a full script file:

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystemProduct")

For Each objItem in colItems 

    Wscript.Echo objItem.Caption
    Wscript.Echo objItem.Description
    Wscript.Echo objItem.IdentifyingNumber
    Wscript.Echo objItem.Name
    Wscript.Echo objItem.SKUNumber
    Wscript.Echo objItem.UUID
    Wscript.Echo objItem.Vendor
    Wscript.Echo objItem.Version

Results got (Correspond to my computer):
Property Value (My Computer)
Caption Computer System Product
Description Computer System Product
IdentifyingNumber 3F027935U
Name Salellite S75B
SKUNumber null
UUID B09366C5-F0C7-E411-98E4-008CFA8C26DF
Version PSPPJU-07U051
See more Win32_ComputerSystemProduct:
Some other Visual Basic classes, you may be interested in:

3- Use Java to get computer hardware information

OK, above you know how to use vb script to retrieve the computer hardware information. Now you need to use the Java to execute vb script files and get the value returned.

package org.o7planning.hardwareinfo;

public class MyUtility {

    public static String makeVbScript(String vbClassName, String[] propNames) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < propNames.length; i++) {
            if (i < propNames.length - 1) {
            } else {
        String colNameString = sb.toString();

        sb.append("Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")").append("\n");
        sb.append("Set colItems = objWMIService.ExecQuery _ ").append("\n");
        sb.append("(\"Select ").append(colNameString).append(" from ").append(vbClassName).append("\") ").append("\n");
        sb.append("For Each objItem in colItems ").append("\n");
        for (String propName : propNames) {
            sb.append("    Wscript.Echo objItem.").append(propName).append("\n");
        sb.append("Next ").append("\n");
        return sb.toString();


package org.o7planning.hardwareinfo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class GetHardwareInfo {

    public static void printComputerSystemProductInfo() {

        String vbClassName = "Win32_ComputerSystemProduct";
        String[] propNames = new String[] { "Name", "UUID", "Vendor", "Version" };

        String vbScript = MyUtility.makeVbScript(vbClassName, propNames);


        try {
            // Create temporary file.
            File file = File.createTempFile("vbsfile", ".vbs");
            System.out.println("Create File: " + file.getAbsolutePath());

            // Write script content to file.
            FileWriter fw = new FileWriter(file);

            // Execute the file.
            Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());

            // Create Input stream to read data returned after execute vb script file.
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

            Map<String, String> map = new HashMap<String, String>();
            String line;
            int i = 0;
            while ((line = input.readLine()) != null) {
                if (i >= propNames.length) {
                String key = propNames[i];
                map.put(key, line);
            for (String propName : propNames) {
                System.out.println(propName + " : " + map.get(propName));

        } catch (Exception e) {

    public static void main(String[] args) {
Run the example:

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.