Getting the System specific Details using java API:
In this topic I want to write a sample code to
get the system specific information by using java interface OperatingSystemMXBean which is available in com.sun.management package.
Copy the below code and place it in eclipse
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import com.sun.management.OperatingSystemMXBean;
@SuppressWarnings("restriction")
public class Test {
public static void main(String[] args) throws Exception {
OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
System.out.println("====CPU Information===");
osBean.getSystemLoadAverage();
osBean.getSystemCpuLoad();
Thread.sleep(5000);
System.out.println("Number of cores, :" + osBean.getAvailableProcessors());
System.out.println("CPU LOAD :" + osBean.getSystemCpuLoad());
System.out.println("===Memory Details==");
System.out.println("TOTAL MEMORY "+ osBean.getTotalPhysicalMemorySize());
System.out.println("AVialable " + osBean.getFreePhysicalMemorySize());
Long toalDiskSpace = 0L;
Long freeSpace = 0L;
Long converter = 1073741824L;
for (FileStore store : FileSystems.getDefault().getFileStores()) {
System.out.println(store);
System.out.println("\t" + store.name());
System.out.println("\t" + store.type());
System.out.println("\t Total Space" + (store.getTotalSpace() / converter) + "GB");
System.out.println("\t Free Space" + (store.getUnallocatedSpace() / converter) + "GB");
toalDiskSpace = toalDiskSpace + store.getTotalSpace();
freeSpace = freeSpace + store.getUnallocatedSpace();
}
System.out.println("===Disk Details===");
System.out.println("toalDiskSpace " + (toalDiskSpace / converter)+ "GB");
System.out.println("freeSpace " + (freeSpace / converter) + "GB");
System.out.println("Used Space " + (toalDiskSpace - freeSpace)/ converter + "GB");
}
}
You will get some errors so in eclipse click on
window -> preferences -> java -> compiler -> Errors/Warnings -> Deprecated
and restricted API change the drop down values to warning -> click on Apply
- >ok
You can find the API
https://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html
You will get some errors so in eclipse click on
window -> preferences -> java -> compiler -> Errors/Warnings -> Deprecated
and restricted API change the drop down values to warning -> click on Apply
- >ok
You can find the API
https://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html