Java get computer IP, MAC, various versions

Source: Internet
Author: User

Package Com.shx.mac;

Import Java.io.BufferedReader;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import java.net.InetAddress;
Import Java.net.NetworkInterface;
Import java.util.ArrayList;
Import Java.util.Formatter;
Import java.util.List;
Import Java.util.Locale;
Import Java.util.Map;
Import java.util.Properties;

public class Systemutil {

Computer configuration information is obtained by intercepting the Cmd stream (bad)
public static list<string> getipaddress () {
Process p = null;
list<string> address = new arraylist<string> ();
try{
p = new Processbuilder ("ipconfig", "/all"). Start ();
}catch (Exception e) {
return address;
}
StringBuffer sb = new StringBuffer ();
Read process output value
InputStream InputStream = P.getinputstream ();
BufferedReader br = new BufferedReader (new InputStreamReader (InputStream));
String s = "";
try{
while ((S=br.readline ())!=null) {
Sb.append (s+ "\ n");
}
}catch (Exception e) {
E.printstacktrace ();
}finally{
try{
Inputstream.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
System.out.println (SB);
return address;
}

public static void Getipconfig () {
map<string,string> map = system.getenv ();
System.out.println (Map.get ("USERNAME"));//Get User name
System.out.println (Map.get ("COMPUTERNAME"));//Get Computer name
System.out.println (Map.get ("UserDomain"));//Get computer domain name
}
Get the computer's IP address and MAC address
public static void GetConfig () {
try{
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);
}catch (Exception e) {
E.printstacktrace ();
}
}
Get the computer's IP, name, operating system name, operating system version
public static void Config () {
try{
InetAddress addr = Inetaddress.getlocalhost ();
String ip=addr.gethostaddress (). toString (); Get the native IP
String hostname=addr.gethostname (). toString (); Get the native computer name
System.out.println ("Native IP:" +ip+ "\ n Native Name:" +hostname);
Properties props=system.getproperties ();
System.out.println ("Name of the operating system:" +props.getproperty ("Os.name"));
SYSTEM.OUT.PRINTLN ("OS Version:" +props.getproperty ("os.version"));
}catch (Exception e) {
E.printstacktrace ();
}
}

Java environment
public static void All () {
Properties props=system.getproperties ();
SYSTEM.OUT.PRINTLN ("Running environment version of Java:" +props.getproperty ("java.version"));
SYSTEM.OUT.PRINTLN ("Running environment provider for Java:" +props.getproperty ("Java.vendor"));
System.out.println ("Java Vendor URL:" +props.getproperty ("Java.vendor.url"));
SYSTEM.OUT.PRINTLN ("Installation path for Java:" +props.getproperty ("Java.home"));
System.out.println ("Java version of the virtual machine specification:" +props.getproperty ("java.vm.specification.version"));
SYSTEM.OUT.PRINTLN ("Virtual machine specification Provider for Java:" +props.getproperty ("Java.vm.specification.vendor"));
SYSTEM.OUT.PRINTLN ("Java Virtual machine Specification Name:" +props.getproperty ("Java.vm.specification.name"));
SYSTEM.OUT.PRINTLN ("Virtual machine implementation version of Java:" +props.getproperty ("java.vm.version"));
SYSTEM.OUT.PRINTLN ("Java Virtual machine Implementation provider:" +props.getproperty ("Java.vm.vendor"));
SYSTEM.OUT.PRINTLN ("Java Virtual machine implementation name:" +props.getproperty ("Java.vm.name"));
System.out.println ("Java Runtime Environment Specification version:" +props.getproperty ("java.specification.version"));
System.out.println ("Java Runtime Environment Specification vendor:" +props.getproperty ("Java.specification.vender"));
System.out.println ("Java Runtime Environment Specification name:" +props.getproperty ("Java.specification.name"));
System.out.println ("Java Class format version number:" +props.getproperty ("java.class.version"));
System.out.println ("Java classpath:" +props.getproperty ("Java.class.path"));
SYSTEM.OUT.PRINTLN ("List of paths searched when loading library:" +props.getproperty ("Java.library.path"));
SYSTEM.OUT.PRINTLN ("Default temporary file path:" +props.getproperty ("Java.io.tmpdir"));
SYSTEM.OUT.PRINTLN ("Path to one or more extended directories:" +props.getproperty ("Java.ext.dirs"));
System.out.println ("Name of the operating system:" +props.getproperty ("Os.name"));
SYSTEM.OUT.PRINTLN ("Architecture of the Operating system:" +props.getproperty ("Os.arch"));
SYSTEM.OUT.PRINTLN ("OS Version:" +props.getproperty ("os.version"));
System.out.println ("File delimiter:" +props.getproperty ("File.separator"));//In Unix system is "/" System.out.println ("Path delimiter:" + Props.getproperty ("Path.separator"));//In Unix system is ":" System.out.println ("line delimiter:" +props.getproperty (" Line.separator "));//In Unix system is"/n "System.out.println (" User's account name: "+props.getproperty (" User.Name "));
System.out.println ("User's home directory:" +props.getproperty ("User.home"));
System.out.println ("User's current working directory:" +props.getproperty ("User.dir"));
}
public static void Main (string[] args) {
Config ();
GetConfig ();
All ();
}
}

Original http://www.cnblogs.com/azhqiang/p/4157965.html

Java get computer IP, MAC, various versions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.