2018/03/14

How To Get System Hardware Information Using Java (Windows Only)



There is no any pure Java code to get all system hardware information.
Although we can get some system details using Java like processor, system architecture and the number of processors.
By the code below :
System.out.println(System.getenv("PROCESSOR_IDENTIFIER")); System.out.println(System.getenv("PROCESSOR_ARCHITECTURE")); System.out.println(System.getenv("PROCESSOR_ARCHITEW6432")); System.out.println(System.getenv("NUMBER_OF_PROCESSORS"));
If we want get all hardware information like hard disk, RAM, CD/DVD, network card, BIOS, monitor and others.
We have to use some third party API or windows tools.

1- Use API :
Using other APIs is sometimes complex to use in our code and configuration is not easy, You can use API like SIGAR download from here.

2- Use Windows tools :
You can get hardware information using Windows tools in your java code.
Example :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

// Get Hardware Information by CMD commends
public class sss {
 public static String HardDriveSerail () {
  String data = "";


  String[][] commands = new String[][] {         {"CMD", "/C", "WMIC OS GET Installdate,SerialNumber"},
                                                 {"CMD", "/C", "WMIC BASEBOARD GET SerialNumber"},
                                                 {"CMD", "/C", "WMIC CPU GET ProcessorId"},
                                                 {"CMD", "/C", "WMIC COMPUTERSYSTEM GET Name"},
                                                 {"CMD", "/C", "WMIC diskdrive GET Name, Manufacturer, Model, InterfaceType, MediaLoaded, MediaType"},
                                                 {"CMD", "/C", "WMIC memphysical GET Manufacturer, Model, SerialNumber, MaxCapacity, MemoryDevices"},
               };
  try {
   for (int i = 0; i < commands.length; i++) {
    String[] com = commands[i];
    Process process = Runtime.getRuntime().exec(com);
        //Closing output stream of the process
    process.getOutputStream().close();
 
    //Reading sucessful output of the command
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String s;
    while ((s = reader.readLine()) != null) {
     data += s;
    }
    //data = reader.lines().collect(Collectors.joining());
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // Print Data
  return data.trim().replaceAll(" +","-");
 }
}

Detailed about WMIC you can visit here.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More