使用Java获取系统信息的常用代码整理总结
1.获取cpu和内存信息
import java.io.inputstreamreader; import java.io.linenumberreader; import java.util.arraylist; import java.util.list; import mytools.com.sun.management.operatingsystemmxbean; import mytools.java.io.file; import mytools.java.lang.management.managementfactory; /** * 获取windows系统信息(cpu,内存,文件系统) * @author libing * */ public class windowsinfoutil { private static final int 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.println(getmemery()); system.out.println(getdisk()); } //获取内存使用率 public static string getmemery(){ operatingsystemmxbean osmxb = (operatingsystemmxbean) managementfactory.getoperatingsystemmxbean(); // 总的物理内存+虚拟内存 long totalvirtualmemory = osmxb.gettotalswapspacesize(); // 剩余的物理内存 long freephysicalmemorysize = osmxb.getfreephysicalmemorysize(); double compare=(double)(1-freephysicalmemorysize*1.0/totalvirtualmemory)*100; string str="内存已使用:"+compare.intvalue()+"%"; return str; } //获取文件系统使用率 public static list<string> getdisk() { // 操作系统 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+":盘 已使用 "+compare.intvalue()+"%"; list.add(str); } } return list; } //获得cpu使用率 public static string getcpuratioforwindows() { try { string proccmd = system.getenv("windir") + "//system32//wbem//wmic.exe process get caption,commandline,kernelmodetime,readoperationcount,threadcount,usermodetime,writeoperationcount"; // 取进程信息 long[] c0 = readcpu(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使用率:"+double.valueof(percent * (busytime)*1.0 / (busytime + idletime)).intvalue()+"%"; } else { return "cpu使用率:"+0+"%"; } } catch (exception ex) { ex.printstacktrace(); return "cpu使用率:"+0+"%"; } } //读取cpu相关信息 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 cmdidx = line.indexof("commandline"); int rocidx = line.indexof("readoperationcount"); int umtidx = line.indexof("usermodetime"); int kmtidx = line.indexof("kernelmodetime"); int wocidx = line.indexof("writeoperationcount"); long idletime = 0; long kneltime = 0; long usertime = 0; while ((line = input.readline()) != null) { if (line.length() < wocidx) { continue; } // 字段出现顺序: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 (exception e) { e.printstacktrace(); } } return null; } /** * 由于string.substring对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下: * @param src 要截取的字符串 * @param start_idx 开始坐标(包括该坐标) * @param end_idx 截止坐标(包括该坐标) * @return */ private static string substring(string src, int start_idx, int end_idx) { byte[] b = src.getbytes(); string tgt = ""; for (int i = start_idx; i <= end_idx; i++) { tgt += (char) b[i]; } return tgt; } }
2.获取本机的ip地址:
private static string getipaddress() throws unknownhostexception { inetaddress address = inetaddress.getlocalhost(); return address.gethostaddress(); }
3.获得网卡地址
public static string getmacaddress(){ string address = ""; string os = system.getproperty("os.name"); string osuser=system.getproperty("user.name"); if (os != null && os.startswith("windows")) { try { string command = "cmd.exe /c ipconfig /all"; process p = runtime.getruntime().exec(command); bufferedreader br =new 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.获得操作系统帐号
string osuser=system.getproperty("user.name");
5.获得操作系统版本
import java.util.properties; properties props=system.getproperties(); //获得系统属性集 string osname = props.getproperty("os.name"); //操作系统名称 string osarch = props.getproperty("os.arch"); //操作系统构架 string osversion = props.getproperty("os.version"); //操作系统版本
6.一些常用的信息获得方式整理
java.version java 运行时环境版本
java.vendor java 运行时环境供应商
java.vendor.url java 供应商的 url
java.home java 安装目录
java.vm.specification.version java 虚拟机规范版本
java.vm.specification.vendor java 虚拟机规范供应商
java.vm.specification.name java 虚拟机规范名称
java.vm.version java 虚拟机实现版本
java.vm.vendor java 虚拟机实现供应商
java.vm.name java 虚拟机实现名称
java.specification.version java 运行时环境规范版本
java.specification.vendor java 运行时环境规范供应商
java.specification.name java 运行时环境规范名称
java.class.version java 类格式版本号
java.class.path java 类路径
java.library.path 加载库时搜索的路径列表
java.io.tmpdir 默认的临时文件路径
java.compiler 要使用的 jit 编译器的名称
java.ext.dirs 一个或多个扩展目录的路径
os.name 操作系统的名称
os.arch 操作系统的架构
os.version 操作系统的版本
file.separator 文件分隔符(在 unix 系统中是“/”)
path.separator 路径分隔符(在 unix 系统中是“:”)
line.separator 行分隔符(在 unix 系统中是“/n”)
user.name 用户的账户名称
user.home 用户的主目录
user.dir 用户的当前工作目录