1. Obtain CPU and memory information
import java.io.InputStreamReader;import java.io.LineNumberReader;import java.util.ArrayList;import java.util.List;import mytools.com.sun.managemen t.OperatingSystemMXBean;import mytools.java.io.File;import mytools .java.lang.management.ManagementFactory;/** * Get windows system information (CPU, memory, file system) * @author libing * */public class WindowsInfoUtil { private static final i nt CPUTIME = 500; private static final int PERCENT = 100; private static final int FAULTLENGTH = 10; public static void main(String[] args) { System.out.println(getCpuRatioForWindows()); System.out.print ln(getMemery()); System.out.println(getDisk ()); } //Get memory usage public static String getMemery(){ OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); / / Total physical memory + virtual memory long totalvirtualMemory = osmxb.getTotalSwapSpaceSize(); // Remaining physical memory long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize(); Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtu alMemory)*100; String str="Memory has been used:"+compare.intValue()+"%" ; return str; } //Get file system usage public static List<String> getDisk() { // Operating system List<String> list=new ArrayList<String>(); for (char c = 'A'; c <= 'Z'; c++) { String dirName = c + ":/"; File win = new File(dirName); if(win.exists()){ long total=(long)win.getTotalSpace(); long free=(long)win.getFreeSpace(); Double compare=(Double)(1-free*1.0/total)*100; String str=c+": The disk has used "+compare.intValue()+"%"; list.add(str); } } return list; } //Get cpu usage public static String getCpuRatioForWindows() { try { String procCmd = System.getenv("windir") + "//s ystem32//wbem//wmic .exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount"; //Fetch process information long[] c0 = readCp u(Runtime.getRuntime().exec(procCmd)); Thread.sleep(CPUTIME); long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd)); if (c0 != null && c1 != null) { long idletime = c1[0] - c0[0]; long busytime = c1[ 1] - c0[1]; return "CPU usage:"+Double.valueOf(PERCENT * (busytime)*1.0 / (busytime + idletime)).intValue()+"%"; } else { return "CPU usage Rate:"+0+"%"; } } catch (Exception ex) { ex.printStackTrace(); return "CPU usage:"+0+"%"; } } //Read CPU-related information private static long [] readCpu(final Process proc) { long[] retn = new long[2]; try { proc.getOutputStream().close(); InputStreamReader ir = new InputStreamReader(proc. getInputStream()); LineNumberReader input = new LineNumberReader (ir); String line = input.readLine(); if (line == null || line.length() < FAULTLENGTH) { return null; } int capidx = line.indexOf("Caption"); int cm didx = line .indexOf("CommandLine"); int rocidx = line.indexOf("ReadOperationCount"); int umtidx = line.indexOf("UserModeTime"); int kmtidx = line.indexO f("KernelModeTime"); int wocidx = line.indexOf ("WriteOperationCount"); long idletime = 0; long kneltime = 0; long usertime = 0; while ((line = input.readLine()) != null) { if (line.length() < woci dx) { continue; } // Fields appearing order: Caption, CommandLine, KernelModeTime, ReadOperationCount, // ThreadCount, UserModeTime, WriteOperation String caption =substring(line, capidx , cmdidx - 1).trim(); String cmd = substring(line, cmdidx, kmtidx - 1).trim(); if (cmd.indexOf("wmic.exe") >= 0) { continue; } String s1 = substring(line, kmtidx, rocidx - 1).trim(); String s2 = substring (line, umtidx, wocidx - 1).trim(); if (caption.equals("System Idle Process") || caption.equals("System")) { if (s1.length() > 0) idletime + = Long.valueOf(s1).longValue(); if (s2.length() > 0) idletime += Long.valueOf(s2).longValue(); continue; } if (s1.length() > 0) kneltime += Long.valueOf(s1).longValue(); if (s2.length() > 0) usertime += Long.valueOf(s2).longValue(); } retn[0] = idletime; retn[1] = kneltime + usertime; return retn; } catch (Exception ex) { ex.printStackTrace(); } finally { try { proc.getInputStream().close(); } catch (Ex ception e) { e.printStackTrace(); } } return null; } /** * Since String.subString has problems with Chinese character processing (considering a Chinese character as a byte), there are hidden dangers when containing Chinese characters. The adjustment is as follows: * @param src String * @param start_idx Start coordinate (including this coordinate) * @param end_idx Cut-off coordinate (including this coordinate) * @return */ private static String substring(String src, int start_idx, int end_id x) { byte[] b = src .getBytes(); String tgt = ""; for (int i = start_idx; i <= end_idx; i++) { tgt += (char) b[i]; } return tgt; }}2. Get the IP address of the machine:
private static String getIpAddress() throws UnknownHostException { InetAddress address = InetAddress.getLocalHost(); return address.getHostAddres s(); }
3. Obtain the network card address
public static String getMACAddress(){ String address = ""; String os = System.getProperty("os.name"); String osUser=System.getProperty("user.na me"); if (os != null && os. startsWith("Windows")) { try { String command = "cmd.exe /c ipconfig /all"; Process p = Runtime.getRuntime().exec(command); BufferedReader br =ne w BufferedReader(new InputStreamReader(p.getInputStream ())); String line; while ((line = br.readLine()) != null) { if (line.indexOf("Physical Address") > 0) { int index = line.indexOf(":") ; index += 2; address = line.substring(index); break; } } br.close(); return address.trim(); } catch (IOException e) { } } return address; }4. Obtain an operating system account
String osUser=System.getProperty("user.name");5. Obtain the operating system version
import java.util.Properties; Properties props=System.getProperties(); //Get system property set String osName = props.getProperty("os.name"); //Operating system name Str ing osArch = props.getProperty("os .arch"); //Operating system architecture String osVersion = props.getProperty("os.version"); //Operating system version
6. Organize some commonly used information acquisition methods
java.version Java runtime environment version
java.vendor Java runtime environment vendor
java.vendor.url The URL of the Java vendor
java.home Java installation directory
java.vm.specification.version Java Virtual Machine Specification Version
java.vm.specification.vendor Java Virtual Machine Specification Vendor
java.vm.specification.name Java virtual machine specification name
java.vm.version Java virtual machine implementation version
java.vm.vendor Java virtual machine implementation vendor
java.vm.name Java virtual machine implementation name
java.specification.version Java runtime environment specification version
java.specification.vendor Java Runtime Environment Specification Vendor
java.specification.name Java runtime environment specification name
java.class.version Java class format version number
java.class.path Java classpath
java.library.path List of paths searched when loading the library
java.io.tmpdir The default temporary file path
The name of the JIT compiler to use by java.compiler
java.ext.dirs The path to one or more extension directories
os.name The name of the operating system
os.arch operating system architecture
os.version operating system version
file.separator file separator ("/" in UNIX systems)
path.separator path separator (":" in UNIX systems)
line.separator line separator ("/n" in UNIX systems)
user.name user's account name
user.home user's home directory
user.dir The current working directory of the user