Showing posts with label Java Memory. Show all posts
Showing posts with label Java Memory. Show all posts

Fetch Windows OS, System, Memory, BIOS, Network, Hard Drive details using Java code

Fetch Windows System details

In lot of applications or real time programs we may in need of finding the Windows system information's like

1. Windows system properties like fetching Windows OS version, JVM version, OS architecture, Java Version, OS name, Windows user name etc.,
2. Fetching Windows BIOS serial number.
3. Fetching memory details like 

  • Virtual Memory Size 
  • Free Physical Memory Size 
  • Free Swap Space Size 
  • Process CPU Time 
  • Total Physical Memory Size 
  • Total Swap Space Size 

4. Fetching network interface and Adapter details 
5. Fetching system hard drive details like each drive total and free space. 

Lets see a Java code which will fetch a complete information as we discussed above.


import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Scanner;

public class FindWindows {

 public static void main(String[] args) {
  
  System.out.println("\n *********************** SYSTEM DETAILS ***********************  ");
  systemDetails();
  System.out.println("\n *********************** MEMORY DETAILS ***********************  ");
  getMemoryDetails();
  System.out.println("\n *********************** BIOS SERIAL NUMBER ***********************  ");
  getBIOSSerialNumber();
  System.out.println("\n *********************** NETWORK DETAILS ***********************  ");
  getNetworkDetails();
  System.out.println("\n *********************** HARD DRIVE DETAILS ***********************  ");
  getHardDriveDetails();
 }

 public static void systemDetails() {
  System.getProperties().list(System.out);
 }

 private static void getMemoryDetails() {
  
  OperatingSystemMXBean osMemDetails = ManagementFactory
    .getOperatingSystemMXBean();
  
  for (Method method : osMemDetails.getClass().getDeclaredMethods()) {
   method.setAccessible(true);
   long value = 0;
   try {
    value = method.invoke(osMemDetails) != null ? Long
      .parseLong(method.invoke(osMemDetails).toString()) : 0;
   } catch (Exception e) {
    e.printStackTrace();
   }
   
   System.out.println(method.getName() + " : " + value+" Bytes");
  }
  
  // no. of processors available
     System.out.println("No. of processors : " + Runtime.getRuntime().availableProcessors());
 }

 public static void getBIOSSerialNumber() {
  try{
   Process process = Runtime.getRuntime().exec(
     new String[] { "wmic", "bios", "get", "serialnumber" });
   
   process.getOutputStream().close();
   Scanner sc = new Scanner(process.getInputStream());
   String property = sc.next();
   String serial = sc.next();
   
   System.out.println(property + ": " + serial);
  }catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static void getNetworkDetails() {
  try{
   Enumeration<NetworkInterface> netIntList = NetworkInterface
     .getNetworkInterfaces();
   while (netIntList.hasMoreElements()) {
    NetworkInterface netInt = netIntList.nextElement();
    System.out.println(netInt.getName() + " : " + netInt.getDisplayName());
   }
  }catch (IOException e) {
   e.printStackTrace();
  }
 }

 public static void getHardDriveDetails() {
  File[] drives = File.listRoots();
  for (int i = 0; i < drives.length; i++) {
   System.out.println("Drive :" + drives[i]);
   File drive = new File(drives[i].toString());
   long totalDriSpace = drive.getTotalSpace();
   long freeSpace = drive.getFreeSpace();
   
   totalDriSpace = (totalDriSpace/1024/1024/1024); // Converting Bytes to GB
   freeSpace = (freeSpace/1024/1024/1024); // Converting Bytes to GB
   
   System.out.println("Total Drive Size : " + totalDriSpace+" GB");
   System.out.println("Free Space       : " + freeSpace+" GB");
   System.out.println("-------------------------------------");
  }
 }
}


OUTPUT:


 *********************** SYSTEM DETAILS ***********************  
-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:\Program Files\Java\jre6\bin
java.vm.version=16.3-b01
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=Service Pack 1
java.vm.specification.name=Java Virtual Machine Specification
user.dir=D:\workspace\java\MyProject
java.runtime.version=1.6.0_20-b02
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs=C:\Program Files\Java\jre6\lib\endorsed
os.arch=x86
java.io.tmpdir=C:\Users\anand\AppData\Local\Temp\
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
user.variant=
os.name=Windows 7
sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jre6\bin;.;C:\W...
java.specification.name=Java Platform API Specification
java.class.version=50.0
sun.management.compiler=HotSpot Client Compiler
os.version=6.1
user.home=C:\Users\anand
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.6
user.name=anand
java.class.path=D:\workspace\java\MyProject\bin;D:\wo...
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=C:\Program Files\Java\jre6
java.specification.vendor=Sun Microsystems Inc.
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.6.0_20
java.ext.dirs=C:\Program Files\Java\jre6\lib\ext;C:...
sun.boot.class.path=C:\Program Files\Java\jre6\lib\resour...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m...

 *********************** MEMORY DETAILS ***********************  
getCommittedVirtualMemorySize : 41385984 Bytes
getCommittedVirtualMemorySize0 : 41385984 Bytes
getFreePhysicalMemorySize : 794828800 Bytes
getFreeSwapSpaceSize : 4186632192 Bytes
getProcessCpuTime : 93600600 Bytes
getTotalPhysicalMemorySize : 2147483647 Bytes
getTotalSwapSpaceSize : 4294967295 Bytes
initialize : 0 Bytes
No. of processors : 2

 *********************** BIOS SERIAL NUMBER ***********************  
SerialNumber: 993D1BS

 *********************** NETWORK DETAILS ***********************  
lo : Software Loopback Interface 1
net0 : WAN Miniport (SSTP)
net1 : WAN Miniport (L2TP)
net2 : WAN Miniport (PPTP)
ppp0 : WAN Miniport (PPPOE)
eth0 : WAN Miniport (IPv6)
eth1 : WAN Miniport (Network Monitor)
eth2 : WAN Miniport (IP)
ppp1 : RAS Async Adapter
eth3 : Intel(R) 82567LM Gigabit Network Connection
net3 : Teredo Tunneling Pseudo-Interface
net4 : Intel(R)  WiFi Link 5300 AGN
eth4 : WAN Miniport (IP) - Juniper Network Agent Miniport
eth5 : Intel(R)  WiFi Link 5300 AGN - Juniper Network Agent Miniport
net5 : Juniper Networks Virtual Adapter Manager
eth6 : Juniper Networks Virtual Adapter
eth7 : Juniper Networks Virtual Adapter - Juniper Network Agent Miniport
eth8 : Juniper Networks Virtual Adapter #2
eth9 : Juniper Networks Virtual Adapter #2 - Juniper Network Agent Miniport
eth10 : Juniper Networks Virtual Adapter #3
eth11 : Juniper Networks Virtual Adapter #3 - Juniper Network Agent Miniport
eth12 : Juniper Networks Virtual Adapter #4
eth13 : Juniper Networks Virtual Adapter #4 - Juniper Network Agent Miniport
net6 : WAN Miniport (IKEv2)
eth14 : Juniper Network Connect Virtual Adapter
eth15 : Juniper Network Connect Virtual Adapter - Juniper Network Agent Miniport
net7 : Microsoft 6to4 Adapter
net8 : Microsoft ISATAP Adapter
net9 : Microsoft ISATAP Adapter #3
net10 : Microsoft ISATAP Adapter #2
net11 : Microsoft ISATAP Adapter #4
eth16 : Intel(R) 82567LM Gigabit Network Connection-QoS Packet Scheduler-0000
eth17 : Intel(R) 82567LM Gigabit Network Connection-WFP LightWeight Filter-0000
eth18 : Intel(R)  WiFi Link 5300 AGN - Juniper Network Agent Miniport-QoS Packet Scheduler-0000
eth19 : Intel(R)  WiFi Link 5300 AGN - Juniper Network Agent Miniport-WFP LightWeight Filter-0000
eth20 : WAN Miniport (Network Monitor)-QoS Packet Scheduler-0000
eth21 : Juniper Network Connect Virtual Adapter - Juniper Network Agent Miniport-QoS Packet Scheduler-0000
eth22 : WAN Miniport (IPv6)-QoS Packet Scheduler-0000
net12 : Intel(R)  WiFi Link 5300 AGN-Native WiFi Filter Driver-0000
eth23 : Juniper Network Connect Virtual Adapter - Juniper Network Agent Miniport-WFP LightWeight Filter-0000
eth24 : WAN Miniport (IP) - Juniper Network Agent Miniport-QoS Packet Scheduler-0000

 *********************** HARD DRIVE DETAILS ***********************  
Drive :C:\
Total Drive Size : 68 GB
Free Space       : 27 GB
-------------------------------------
Drive :D:\
Total Drive Size : 80 GB
Free Space       : 72 GB
-------------------------------------
Drive :E:\
Total Drive Size : 0 GB
Free Space       : 0 GB
-------------------------------------
Drive :H:\
Total Drive Size : 4191 GB
Free Space       : 113 GB
-------------------------------------
Drive :M:\
Total Drive Size : 78 GB
Free Space       : 48 GB
-------------------------------------
Drive :N:\
Total Drive Size : 78 GB
Free Space       : 48 GB
-------------------------------------