ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » Java programs » Most popular & searches Java programs

Java program to get CPU Serial number for windows machine

Get CPU Serial Number in Java: This java program will get and print the CPU Serial number for windows machine.

package com.includehelp;

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

/**
 * Program to get CPU Serial number for windows machine
 * @author includehelp
 */
public class WindowsCpuSerialNumber {
    /**
     * Method for get Windows Machine CPU Serial Number
     * @return 
     */
    private static String getWindowsCPU_SerialNumber() {
        String result = "";
        try {
            File file = File.createTempFile("realhowto",".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            
            String vbs1=
              "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                + "Set colItems = objWMIService.ExecQuery _ \n"
                + "   (\"Select * from Win32_Processor\") \n"
                + "For Each objItem in colItems \n"
                + "    Wscript.Echo objItem.ProcessorId \n"
                + "    exit for  ' do the first cpu only! \n"
                + "Next \n";
              

            fw.write(vbs1);
            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){
             System.err.println("Windows CPU Exp : "+E.getMessage());
        }
        return result.trim();
    }
    
    public static void main(String[] args) {
        String cpuSerialNumber = getWindowsCPU_SerialNumber();
        System.out.println("Windows Machine CPU Serial Number : "+cpuSerialNumber);
    }
}

Output

Windows Machine CPU Serial Number : AZ30FABC0ZXC9823
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.