1)最简单的获取本机的机器名
import java.net.InetAddress; import java.net.UnknownHostException; public class GetInfo { public static void main(String[] args) throws UnknownHostException { InetAddress s = InetAddress.getLocalHost(); System.out.println(s.getHostName()); } }输入结果:
PC-201012262159
2)获取Java环境信息和操作系统环境信息
import java.util.*; public class GetInfo { public static void main(String[] args) { Properties props = System.getProperties(); System.out.println("Java的运行环境版本:" + props.getProperty("java.version")); System.out.println("Java的运行环境供应商:" + props.getProperty("java.vendor")); System.out.println("Java供应商的URL:"+ props.getProperty("java.vendor.url")); System.out.println("Java的安装路径:" + props.getProperty("java.home")); System.out.println("Java的虚拟机规范版本:"+ props.getProperty("java.vm.specification.version")); System.out.println("Java的虚拟机规范供应商:" + props.getProperty("java.vm.specification.vendor")); System.out.println("Java的虚拟机规范名称:"+ props.getProperty("java.vm.specification.name")); System.out.println("Java的虚拟机实现版本:"+ props.getProperty("java.vm.version")); System.out.println("Java的虚拟机实现供应商:" + props.getProperty("java.vm.vendor")); System.out.println("Java的虚拟机实现名称:" + props.getProperty("java.vm.name")); System.out.println("Java运行时环境规范版本:" + props.getProperty("java.specification.version")); System.out.println("Java运行时环境规范供应商:"+ props.getProperty("java.specification.vender")); System.out.println("Java运行时环境规范名称:" + props.getProperty("java.specification.name")); System.out.println("Java的类格式版本号:"+ props.getProperty("java.class.version")); System.out.println("Java的类路径:" + props.getProperty("java.class.path")); System.out.println("加载库时搜索的路径列表:" + props.getProperty("java.library.path")); System.out.println("默认的临时文件路径:" + props.getProperty("java.io.tmpdir")); System.out.println("一个或多个扩展目录的路径:" + props.getProperty("java.ext.dirs")); System.out.println("操作系统的名称:" + props.getProperty("os.name")); System.out.println("操作系统的构架:" + props.getProperty("os.arch")); System.out.println("操作系统的版本:" + props.getProperty("os.version")); 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")); System.out.println("用户的主目录:" + props.getProperty("user.home")); System.out.println("用户的当前工作目录:" + props.getProperty("user.dir")); } }
输出结果:
Java的运行环境版本:1.6.0_13
Java的运行环境供应商:Sun Microsystems Inc.
Java供应商的URL:http://java.sun.com/
Java的安装路径:D:\Program Files\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre
Java的虚拟机规范版本:1.0
Java的虚拟机规范供应商:Sun Microsystems Inc.
Java的虚拟机规范名称:Java Virtual Machine Specification
Java的虚拟机实现版本:11.3-b02
Java的虚拟机实现供应商:Sun Microsystems Inc.
Java的虚拟机实现名称:Java HotSpot(TM) Client VM
Java运行时环境规范版本:1.6
Java运行时环境规范供应商:null
Java运行时环境规范名称:Java Platform API Specification
Java的类格式版本号:50.0
Java的类路径:C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\classes;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\core\org.springframework.core-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\core\org.springframework.context-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\core\org.springframework.beans-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\core\org.springframework.asm-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\core\com.springsource.org.apache.log4j-1.2.15.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\core\com.springsource.org.apache.commons.logging-1.1.1.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\core\org.springframework.expression-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistJdbc\com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistJdbc\commons-dbcp.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistJdbc\com.springsource.org.apache.commons.pool-1.5.3.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistJdbc\jotm.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistJdbc\xapool.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistJdbc\com.springsource.org.apache.commons.lang-2.4.0.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistCore\persistence.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistCore\org.springframework.jdbc-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistCore\org.springframework.orm-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\persistCore\org.springframework.transaction-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\aop\org.springframework.aop-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\aop\com.springsource.org.aopalliance-1.0.0.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\aop\com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\aop\com.springsource.net.sf.cglib-2.2.0.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\aop\org.springframework.aspects-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\aop\org.springframework.instrument-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\MyEclipse 8.5\configuration\org.eclipse.osgi\bundles\87\1\.cp\data\3.0\lib\aop\org.springframework.instrument.tomcat-3.0.1.RELEASE-A.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.ws.xfire_8.5.0.me201003052220\lib\webservices-api.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.ws.xfire_8.5.0.me201003052220\lib\webservices-extra-api.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.ws.xfire_8.5.0.me201003052220\lib\webservices-extra.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.ws.xfire_8.5.0.me201003052220\lib\webservices-rt.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\activation.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\javax.servlet.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\javax.servlet.jsp.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\jboss-j2ee.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\jboss-jaxrpc.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\jboss-jsr77.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\jboss-saaj.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\mail.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\namespace.jar;D:\Program Files\Genuitec\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\1.4\xml-apis.jar;C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\lib\commons-codec-1.4.jar;C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\lib\commons-lang-2.4.jar;C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\lib\dom4j-1.6.1.jar;C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\lib\log4j-1.2.14.jar;C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\lib\commons-io-1.4.jar;C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\lib\jaxen-1.1.1.jar;C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\lib\ojdbc14.jar;C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication\WEB-INF\lib\commons-httpclient-3.1.jar
加载库时搜索的路径列表:D:\Program Files\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:/Program Files/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client;D:/Program Files/Genuitec/Common/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;C:\Program Files\Common Files\NetSarang;F:\oracle\product\10.2.0\db_1\bin;;d:\Program Files\XMedia\tools\miscc:\oracle\ora92\bin;C:\Program Files\Oracle\jre\1.1.8\bin;D:\Android\android-sdk-windows\tools;D:\Program Files\TortoiseSVN\bin;C:\Program Files\Intel\WiFi\bin\;D:\Program Files\Java\jrockit-jdk1.6.0_22-R28.1.1-4.0.1\bin;c:\oracle\ora92\bin;""C:\Program Files\Oracle\jre\1.1.8\bin";"";"
默认的临时文件路径:C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\
一个或多个扩展目录的路径:D:\Program Files\Genuitec\Common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext
操作系统的名称:Windows XP
操作系统的构架:x86
操作系统的版本:5.1
文件分隔符:\
路径分隔符:;
行分隔符:
用户的账户名称:Administrator
用户的主目录:C:\Documents and Settings\Administrator
用户的当前工作目录:C:\Documents and Settings\Administrator\Workspaces\MyEclipse 8.5\Authentication
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Formatter; import java.util.Locale; public class GetInfo { public static void main(String[] args) throws UnknownHostException, SocketException { InetAddress address = InetAddress.getLocalHost(); NetworkInterface ni = NetworkInterface.getByInetAddress(address); ni.getInetAddresses().nextElement().getAddress(); byte[] mac = ni.getHardwareAddress(); String sIP = address.getHostAddress(); String sMAC = ""; Formatter formatter = new Formatter(); for (int i = 0; i < mac.length; i++) { sMAC = formatter.format(Locale.getDefault(), "%02X%s", mac[i], (i < mac.length - 1) ? "-" : "").toString(); } System.out.println("IP:" + sIP); System.out.println("MAC:" + sMAC); } }
输出结果:
IP:10.192.33.64
MAC:00-21-86-58-39-C6
4)获取本机用户名、计算机名、计算机域名
import java.util.Map; public class GetInfo { public static void main(String[] args) { Map<String, String> map = System.getenv(); String userName = map.get("USERNAME");// 获取用户名 String computerName = map.get("COMPUTERNAME");// 获取计算机名 String userDomain = map.get("USERDOMAIN");// 获取计算机域名 System.out.println(userName); System.out.println(computerName); System.out.println(userDomain); } }使用自带的 System.getenv()方法获得一个Map
输出结果:
Administrator
PC-201012262159
PC-201012262159
5)获取本机的实时IP、计算机名、操作系统名、操作系统版本
import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Properties; public class GetInfo { public static void main(String[] args) throws UnknownHostException { InetAddress addr = InetAddress.getLocalHost(); String ip=addr.getHostAddress().toString(); //获取本机ip String hostName=addr.getHostName().toString(); //获取本机计算机名称 System.out.println("本机IP:"+ip+"\n本机名称:"+hostName); Properties props1=System.getProperties(); System.out.println("操作系统的名称:"+props1.getProperty("os.name")); System.out.println("操作系统的版本:"+props1.getProperty("os.version")); } }输出结果:
本机IP:10.192.33.64
本机名称:PC-201012262159
操作系统的名称:Windows XP
操作系统的版本:5.1