|
| 1 | +package cn.aofeng.demo.misc; |
| 2 | + |
| 3 | +import java.net.InetAddress; |
| 4 | +import java.net.NetworkInterface; |
| 5 | +import java.net.SocketException; |
| 6 | +import java.net.UnknownHostException; |
| 7 | +import java.util.Enumeration; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Map.Entry; |
| 10 | +import java.util.Properties; |
| 11 | +import java.util.Set; |
| 12 | + |
| 13 | +/** |
| 14 | + * 获取本机IP和主机名以及Java环境信息。 |
| 15 | + * |
| 16 | + * @author <a href="mailto:[email protected]">聂勇</a> |
| 17 | + */ |
| 18 | +public class GetHostInfo { |
| 19 | + |
| 20 | + /** |
| 21 | + * @param args |
| 22 | + * @throws UnknownHostException |
| 23 | + * @throws SocketException |
| 24 | + */ |
| 25 | + public static void main(String[] args) throws UnknownHostException, SocketException { |
| 26 | + InetAddress address = InetAddress.getLocalHost(); |
| 27 | + System.out.println("计算机名:" + address.getHostName()); |
| 28 | + Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); |
| 29 | + while (nis.hasMoreElements()) { |
| 30 | + StringBuilder buffer = new StringBuilder(); |
| 31 | + NetworkInterface ni = nis.nextElement(); |
| 32 | + buffer.append("网卡:").append(ni.getName()); |
| 33 | + buffer.append(" 绑定IP:"); |
| 34 | + Enumeration<InetAddress> ias = ni.getInetAddresses(); |
| 35 | + int count = 0; |
| 36 | + while (ias.hasMoreElements()) { |
| 37 | + InetAddress ia = ias.nextElement(); |
| 38 | + if (count > 0) { |
| 39 | + buffer.append(", "); |
| 40 | + } |
| 41 | + buffer.append(ia.getHostAddress()); |
| 42 | + } |
| 43 | + System.out.println(buffer.toString()); |
| 44 | + } |
| 45 | + |
| 46 | + System.out.println("Java环境信息:"); |
| 47 | + System.out.println("---------------------------------------------------:"); |
| 48 | + Properties pros = System.getProperties(); |
| 49 | + Set<Entry<Object, Object>> javaEnums = pros.entrySet(); |
| 50 | + for (Entry<Object, Object> entry : javaEnums) { |
| 51 | + System.out.println(entry.getKey() + " : " + entry.getValue()); |
| 52 | + } |
| 53 | + System.out.println(""); |
| 54 | + System.out.println("系统环境信息:"); |
| 55 | + System.out.println("---------------------------------------------------:"); |
| 56 | + Map<String, String> envs = System.getenv(); |
| 57 | + Set<Entry<String, String>> envSet = envs.entrySet(); |
| 58 | + for (Entry<String, String> entry : envSet) { |
| 59 | + System.out.println(entry.getKey() + " : " + entry.getValue()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments