There are several methods for mainstream use, one is to use the following function
publicstaticStringgetProperty(Stringkey)
| key | Description of related values |
|---|---|
| java.version | java.version Java runtime environment version |
| java.vendor | java.vendor Java runtime environment vendor |
| java.vendor.url | java.vendor.url The URL of the Java vendor |
| java.home | java.home Java installation directory |
| java.vm.specification.version | java.vm.specification.version Java Virtual Machine Specification Version |
| java.vm.specification.vendor | java.vm.specification.vendor Java Virtual Machine Specification Vendor |
| java.vm.specification.name | java.vm.specification.name Java virtual machine specification name |
| java.vm.version | java.vm.version Java virtual machine implementation version |
| java.vm.vendor | java.vm.vendor Java virtual machine implementation vendor |
| java.vm.name | java.vm.name Java virtual machine implementation name |
| java.specification.version | java.specification.version Java runtime environment specification version |
| java.specification.vendor | java.specification.vendor Java Runtime Environment Specification Vendor |
| java.specification.name | java.specification.name Java runtime environment specification name |
| java.class.version | java.class.version Java class format version number |
| java.class.path | java.class.path Java classpath |
| java.library.path | java.library.path List of paths searched when loading the library |
| java.io.tmpdir | java.io.tmpdir The default temporary file path |
| java.compiler | The name of the JIT compiler to use by java.compiler |
| java.ext.dirs | java.ext.dirs The path to one or more extension directories |
| os.name | os.name The name of the operating system |
| os.arch | os.arch operating system architecture |
| os.version | os.version operating system version |
| file.separator | file.separator file separator ("/" in UNIX systems) |
| path.separator | path.separator path separator (":" in UNIX systems) |
| line.separator | line.separator line separator ("/n" in UNIX systems) |
| user.name | user.name user's account name |
| user.home | user.home user's home directory |
| user.dir | user.dir The current working directory of the user |
One is to use the command line to obtain it
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.InputStreamReader;import java.net.IntAddress;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 test {//Get the computer's configuration information by intercepting cmd flow (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 the 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 username System.out.println(map.get("COMPUTERNAME"));//Get computer name System.out.println(map.get("USERDOMAIN"));//Get computer domain name}//Get computer 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 native ipString hostName = addr.getHostName().toString();//Get the native computer name System.out.println("Native IP:" + ip + "/nNative name:" + hostName);Properties props = System.getProperties();System.out.println("Operating system name:" + props.getProperty("os.name"));System.out.println("Operating system version:" + props.getProperty("os.version"));}catch (Exception e) {e.printStackTrace();}}//Some other things will be used public static void all() {Properties props = System.getProperties();System.out.println("Java's runtime environment version: " + props.getProperty("java.version"));System.out.println("Java's runtime environment vendor: " + props.getProperty("java.vendor"));System.out.println("Java's runtime vendor: " + props.getProperty("java.vendor.url"));System.out.println("Java's installation path: " + props.getProperty("java.home"));System.out.println("Java virtual machine specification version: " + props.getProperty("java.vm.specification.version"));System.out.println("Java virtual machine specification vendor: " + props.getProperty("java.vm.specification.vendor"));System.out.println("Java virtual machine specification name: " + props.getProperty("java.vm.specification.name"));System.out.println("Java virtual machine specification version: " + props.getProperty("java.vm.specification.name"));System.out.println("Java virtual machine implementation version: " + props.getProperty("java.vm.version"));System.out.println("Java virtual machine implementation vendor: " + 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.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 class path: " + 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 extension directories: " + props.getProperty("java.ext.dirs"));System.out.println("Operation system name: " + props.getProperty("os.name"));System.out.println("Operation system architecture: " + props.getProperty("os.arch"));System.out.println("Operation system version:" + props.getProperty("os.version"));System.out.println("file separator: " + props.getProperty("file.separator"));//In unix system it is "/n"System.out.println("path separator: " + props.getProperty("path.separator"));//In unix system it is ":"System.out.println("line separator: " + props.getProperty("line.separator"));//In unix system it 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) {getConfig();Config();all();}}Summarize
The above is the entire content of this article about Java obtaining the information example code of the current operating system. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!