获取计算机名称,操作系统信息,java信息
1 package com.agen.test1; 2 3 import java.io.BufferedReader; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.net.InetAddress; 7 import java.net.NetworkInterface; 8 import java.util.ArrayList; 9 import java.util.Formatter; 10 import java.util.List; 11 import java.util.Locale; 12 import java.util.Map; 13 import java.util.Properties; 14 15 public class SixTest { 16 17 18 19 //通过截取cmd流方式得到计算机的配置信息(不好) 20 public static List<String> getIpAddress(){ 21 Process p = null; 22 List<String> address = new ArrayList<String>(); 23 try{ 24 p = new ProcessBuilder("ipconfig","/all").start(); 25 }catch(Exception e){ 26 return address; 27 } 28 StringBuffer sb = new StringBuffer(); 29 //读取进程输出值 30 InputStream inputStream = p.getInputStream(); 31 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 32 String s = ""; 33 try{ 34 while((s=br.readLine())!=null){ 35 sb.append(s+"\n"); 36 } 37 }catch(Exception e){ 38 e.printStackTrace(); 39 }finally{ 40 try{ 41 inputStream.close(); 42 }catch(Exception e){ 43 e.printStackTrace(); 44 } 45 } 46 System.out.println(sb); 47 return address; 48 } 49 50 public static void getIpconfig(){ 51 Map<String,String> map = System.getenv(); 52 System.out.println("计算机用户名"+map.get("USERNAME"));//获取用户名 53 System.out.println("计算机名"+map.get("COMPUTERNAME"));//获取计算机名 54 System.out.println("计算机域名"+map.get("USERDOMAIN"));//获取计算机域名 55 } 56 //得到计算机的ip地址和mac地址 57 public static void getConfig(){ 58 try{ 59 InetAddress address = InetAddress.getLocalHost(); 60 NetworkInterface ni = NetworkInterface.getByInetAddress(address); 61 //ni.getInetAddresses().nextElement().getAddress(); 62 byte[] mac = ni.getHardwareAddress(); 63 String sIP = address.getHostAddress(); 64 String sMAC = ""; 65 Formatter formatter = new Formatter(); 66 for (int i = 0; i < mac.length; i++) { 67 sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i], 68 (i < mac.length - 1) ? "-" : "").toString(); 69 70 } 71 System.out.println("IP:" + sIP); 72 System.out.println("MAC:" + sMAC); 73 }catch(Exception e){ 74 e.printStackTrace(); 75 } 76 } 77 //得到计算机的ip,名称,操作系统名称,操作系统版本 78 public static void Config(){ 79 try{ 80 InetAddress addr = InetAddress.getLocalHost(); 81 String ip=addr.getHostAddress().toString(); //获取本机ip 82 String hostName=addr.getHostName().toString(); //获取本机计算机名称 83 System.out.println("本机IP:"+ip+"\n本机名称:"+hostName); 84 Properties props=System.getProperties(); 85 System.out.println("操作系统的名称:"+props.getProperty("os.name")); 86 System.out.println("操作系统的版本:"+props.getProperty("os.version")); 87 }catch(Exception e){ 88 e.printStackTrace(); 89 } 90 } 91 //java环境 92 public static void all(){ 93 Properties props=System.getProperties(); 94 System.out.println("Java的运行环境版本:"+props.getProperty("java.version")); 95 System.out.println("Java的运行环境供应商:"+props.getProperty("java.vendor")); 96 System.out.println("Java供应商的URL:"+props.getProperty("java.vendor.url")); 97 System.out.println("Java的安装路径:"+props.getProperty("java.home")); 98 System.out.println("Java的虚拟机规范版本:"+props.getProperty("java.vm.specification.version")); 99 System.out.println("Java的虚拟机规范供应商:"+props.getProperty("java.vm.specification.vendor")); 100 System.out.println("Java的虚拟机规范名称:"+props.getProperty("java.vm.specification.name")); 101 System.out.println("Java的虚拟机实现版本:"+props.getProperty("java.vm.version")); 102 System.out.println("Java的虚拟机实现供应商:"+props.getProperty("java.vm.vendor")); 103 System.out.println("Java的虚拟机实现名称:"+props.getProperty("java.vm.name")); 104 System.out.println("Java运行时环境规范版本:"+props.getProperty("java.specification.version")); 105 System.out.println("Java运行时环境规范供应商:"+props.getProperty("java.specification.vender")); 106 System.out.println("Java运行时环境规范名称:"+props.getProperty("java.specification.name")); 107 System.out.println("Java的类格式版本号:"+props.getProperty("java.class.version")); 108 System.out.println("Java的类路径:"+props.getProperty("java.class.path")); 109 System.out.println("加载库时搜索的路径列表:"+props.getProperty("java.library.path")); 110 System.out.println("默认的临时文件路径:"+props.getProperty("java.io.tmpdir")); 111 System.out.println("一个或多个扩展目录的路径:"+props.getProperty("java.ext.dirs")); 112 System.out.println("操作系统的名称:"+props.getProperty("os.name")); 113 System.out.println("操作系统的构架:"+props.getProperty("os.arch")); 114 System.out.println("操作系统的版本:"+props.getProperty("os.version")); 115 System.out.println("文件分隔符:"+props.getProperty("file.separator"));//在 unix 系统中是"/" System.out.println("路径分隔符:"+props.getProperty("path.separator"));//在 unix 系统中是":" System.out.println("行分隔符:"+props.getProperty("line.separator"));//在 unix 系统中是"/n" System.out.println("用户的账户名称:"+props.getProperty("user.name")); 116 System.out.println("用户的主目录:"+props.getProperty("user.home")); 117 System.out.println("用户的当前工作目录:"+props.getProperty("user.dir")); 118 } 119 public static void main(String[] args) { 120 getIpconfig(); 121 System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 122 getConfig(); 123 System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 124 Config(); 125 System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 126 all(); 127 } 128 129 130 }