一 .获取系统相关参数
java 通过System.getProperties()获取系统参数
Properties props=System.getProperties(); //系统属性
1 public class SystemInfo { 2 public static void main(String[] args) { 3 Properties sysProperty=System.getProperties(); //系统属性 4 Set<Object> keySet = sysProperty.keySet(); 5 for (Object object : keySet) { 6 String property = sysProperty.getProperty(object.toString()); 7 System.out.println(object.toString()+" : "+property); 8 } 9 } 10 }
汉语英语一一对应
1 public class SystemInfo { 2 public static void main(String[] args) { 3 System.out.println("Java的运行环境版本:"+sysProperty.getProperty("java.version")); 4 System.out.println("Java的运行环境供应商:"+sysProperty.getProperty("java.vendor")); 5 System.out.println("Java供应商的URL:"+sysProperty.getProperty("java.vendor.url")); 6 System.out.println("Java的安装路径:"+sysProperty.getProperty("java.home")); 7 System.out.println("Java的虚拟机规范版本:"+sysProperty.getProperty("java.vm.specification.version")); 8 System.out.println("Java的虚拟机规范供应商:"+sysProperty.getProperty("java.vm.specification.vendor")); 9 System.out.println("Java的虚拟机规范名称:"+sysProperty.getProperty("java.vm.specification.name")); 10 System.out.println("Java的虚拟机实现版本:"+sysProperty.getProperty("java.vm.version")); 11 System.out.println("Java的虚拟机实现供应商:"+sysProperty.getProperty("java.vm.vendor")); 12 System.out.println("Java的虚拟机实现名称:"+sysProperty.getProperty("java.vm.name")); 13 System.out.println("Java运行时环境规范版本:"+sysProperty.getProperty("java.specification.version")); 14 System.out.println("Java运行时环境规范供应商:"+sysProperty.getProperty("java.specification.vender")); 15 System.out.println("Java运行时环境规范名称:"+sysProperty.getProperty("java.specification.name")); 16 System.out.println("Java的类格式版本号:"+sysProperty.getProperty("java.class.version")); 17 System.out.println("Java的类路径:"+sysProperty.getProperty("java.class.path")); 18 System.out.println("加载库时搜索的路径列表:"+sysProperty.getProperty("java.library.path")); 19 System.out.println("默认的临时文件路径:"+sysProperty.getProperty("java.io.tmpdir")); 20 System.out.println("一个或多个扩展目录的路径:"+sysProperty.getProperty("java.ext.dirs")); 21 System.out.println("操作系统的名称:"+sysProperty.getProperty("os.name")); 22 System.out.println("操作系统的构架:"+sysProperty.getProperty("os.arch")); 23 System.out.println("操作系统的版本:"+sysProperty.getProperty("os.version")); 24 System.out.println("文件分隔符:"+sysProperty.getProperty("file.separator")); //在 unix 系统中是"/" 25 System.out.println("路径分隔符:"+sysProperty.getProperty("path.separator")); //在 unix 系统中是":" 26 System.out.println("行分隔符:"+sysProperty.getProperty("line.separator")); //在 unix 系统中是"/n" 27 System.out.println("用户的账户名称:"+sysProperty.getProperty("user.name")); 28 System.out.println("用户的主目录:"+sysProperty.getProperty("user.home")); 29 System.out.println("用户的当前工作目录:"+sysProperty.getProperty("user.dir")); 30 } 31 }
二、获取系统环境变量
Map<String, String> getenv = System.getenv();
Map<String, String> getenv = System.getenv();
System.out.println(getenv);
输出:
{USERPROFILE=C:\Users\kongxiaohan, ProgramData=C:\ProgramData, PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC, windows_tracing_logfile=C:\BVTBin\Tests\installpackage\csilogfile.log, JAVA_HOME=C:\Program Files\Java\jdk1.7.0_65, MAVEN_HOME=D:\apache-maven-3.0.5, windows_tracing_flags=3, TEMP=C:\Users\KONGXI~1\AppData\Local\Temp, SystemDrive=C:, ProgramFiles=C:\Program Files, Path=C:/Program Files/Java/jdk1.7.0_65/bin/../jre/bin/client; C:/Program Files/Java/jdk1.7.0_65/bin/../jre/bin; C:/Program Files/Java/jdk1.7.0_65/bin/../jre/lib/i386; C:\Program Files\NVIDIA Corporation\PhysX\Common; C:\Program Files\Java\jdk1.7.0_65\bin; C:\Windows\system32;C:\Windows; C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\; C:\Program Files\EmEditor; D:\Program Files\MySQL\MySQL Server 5.5\bin; D:\Program Files\Subversion\bin; D:\Program Files\TortoiseSVN\bin; D:\ITCAST\java\day72_NoSql系列之mongoDB数据库\mongodb-win32-i386-2.4.3\bin; E:\insta;tclient_12_1; D:\apache-maven-3.0.5\bin; C:\Program Files\VanDyke Software\Clients\; G:\eclipse;, HOMEDRIVE=C:, PROCESSOR_REVISION=2502, USERDOMAIN=PC-ACER, ALLUSERSPROFILE=C:\ProgramData, VBOX_INSTALL_PATH=C:\Program Files\Oracle\VirtualBox\, PROCESSOR_IDENTIFIER=x86 Family 6 Model 37 Stepping 2, GenuineIntel, SESSIONNAME=Console, TMP=C:\Users\KONGXI~1\AppData\Local\Temp, CommonProgramFiles=C:\Program Files\Common Files, =::=::\, LOGONSERVER=\\PC-ACER, PROCESSOR_ARCHITECTURE=x86, FP_NO_HOST_CHECK=NO, OS=Windows_NT, HOMEPATH=\Users\kongxiaohan, PROCESSOR_LEVEL=6, APR_ICONV_PATH=D:\Program Files\Subversion\iconv, LOCALAPPDATA=C:\Users\kongxiaohan\AppData\Local, COMPUTERNAME=PC-ACER, windir=C:\Windows, SystemRoot=C:\Windows, NUMBER_OF_PROCESSORS=4, USERNAME=kongxiaohan, PUBLIC=C:\Users\Public, PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\, NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK, LANG=zh_CN, ComSpec=C:\Windows\system32\cmd.exe, APPDATA=C:\Users\kongxiaohan\AppData\Roaming}
三、获取IP、 计算机名、MAC、 硬盘卷序号
SystemInfo.java
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.net.InetAddress; 5 import java.net.NetworkInterface; 6 import java.net.SocketException; 7 import java.net.UnknownHostException; 8 import java.util.Properties; 9 10 /** 11 * 获取当前系统信息 12 */ 13 public class SystemInfo { 14 // 当前实例 15 private static SystemInfo currentSystem = null; 16 private InetAddress localHost = null; 17 18 private SystemInfo() { 19 try { 20 localHost = InetAddress.getLocalHost(); 21 } catch (UnknownHostException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 } 26 27 /** 28 * 单例模式获取对象 29 * 30 * @return 31 */ 32 public static SystemInfo getInstance() { 33 if (currentSystem == null) 34 currentSystem = new SystemInfo(); 35 return currentSystem; 36 } 37 38 /** 39 * 本地IP 40 * 41 * @return IP地址 42 */ 43 public String getIP() { 44 String ip = localHost.getHostAddress(); 45 return ip; 46 } 47 48 /** 49 * 获取用户机器名称 50 * 51 * @return 52 */ 53 public String getHostName() { 54 return localHost.getHostName(); 55 } 56 57 /** 58 * 获取C盘卷 序列号 59 * 60 * @return 61 */ 62 public String getDiskNumber() { 63 String line = ""; 64 String HdSerial = "";// 记录硬盘序列号 65 66 try { 67 68 Process proces = Runtime.getRuntime().exec("cmd /c dir c:");// 获取命令行参数 69 BufferedReader buffreader = new BufferedReader( 70 new InputStreamReader(proces.getInputStream())); 71 72 while ((line = buffreader.readLine()) != null) { 73 74 if (line.indexOf("卷的序列号是 ") != -1) { // 读取参数并获取硬盘序列号 75 76 HdSerial = line.substring(line.indexOf("卷的序列号是 ") 77 + "卷的序列号是 ".length(), line.length()); 78 break; 79 } 80 } 81 82 } catch (IOException e) { 83 // TODO Auto-generated catch block 84 e.printStackTrace(); 85 } 86 87 return HdSerial; 88 } 89 90 /** 91 * 获取Mac地址 92 * 93 * @return Mac地址,例如:F0-4D-A2-39-24-A6 94 */ 95 public String getMac() { 96 NetworkInterface byInetAddress; 97 try { 98 byInetAddress = NetworkInterface.getByInetAddress(localHost); 99 byte[] hardwareAddress = byInetAddress.getHardwareAddress(); 100 return getMacFromBytes(hardwareAddress); 101 } catch (SocketException e) { 102 // TODO Auto-generated catch block 103 e.printStackTrace(); 104 } 105 return null; 106 } 107 108 /** 109 * 获取当前系统名称 110 * 111 * @return 当前系统名,例如: windows xp 112 */ 113 public String getSystemName() { 114 Properties sysProperty = System.getProperties(); 115 // 系统名称 116 String systemName = sysProperty.getProperty("os.name"); 117 return systemName; 118 } 119 120 private String getMacFromBytes(byte[] bytes) { 121 StringBuffer mac = new StringBuffer(); 122 byte currentByte; 123 boolean first = false; 124 for (byte b : bytes) { 125 if (first) { 126 mac.append("-"); 127 } 128 currentByte = (byte) ((b & 240) >> 4); 129 mac.append(Integer.toHexString(currentByte)); 130 currentByte = (byte) (b & 15); 131 mac.append(Integer.toHexString(currentByte)); 132 first = true; 133 } 134 return mac.toString().toUpperCase(); 135 } 136 }
SystemInfoTest.java
1 public class SystemInfoTest { 2 public static void main(String[] args) { 3 SystemInfo syso = SystemInfo.getInstance(); 4 System.out.println("IP地址:"+syso.getIP()); 5 System.out.println("主机名称:"+syso.getHostName()); 6 System.out.println("获取C盘卷 序列号"+syso.getDiskNumber()); 7 System.out.println("Mac地址"+syso.getMac()); 8 System.out.println("获取当前系统名称"+syso. getSystemName()); 9 } 10 }
输出结果:
IP地址:169.254.247.195 主机名称:pc-acer 获取C盘卷 序列号A0E7-407C Mac地址08-00-27-00-90-35 获取当前系统名称Windows 7
//============================================================================================
本文转自SummerChill博客园博客,原文链接:http://www.cnblogs.com/DreamDrive/p/4289860.html,如需转载请自行联系原作者