本例以Windows, JDK1.8 为基础,来演示使用Java 获取当前系统属性。
系统属性包括很多,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 java.version //Java version number java.vendor //Java vendor specific string java.vendor.url //Java vendor URL java.home //Java installation directory java.class.version //Java class version number java.class.path //Java classpath os.name //Operating System Name os.arch //Operating System Architecture os.version //Operating System Version file.separator //File separator ("/" on Unix) path.separator //Path separator (":" on Unix) line.separator //Line separator ("\n" on Unix) user.name //User account name user.home //User home directory user.dir //User's current working directory
获取系统指定属性
CurrentSystem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com.devnp.system;public class CurrentSystem { public static void main (String[] args) { System.out.println("Java Version : " + System.getProperty("java.version" )); System.out.println("Java Vendor : " + System.getProperty("java.vendor" )); System.out.println("Java vendor URL : " + System.getProperty("java.vendor.url" )); System.out.println("Java Installation Directory : " + System.getProperty("java.home" )); System.out.println("Java class version number : " + System.getProperty("java.class.version" )); System.out.println("Java classpath : " + System.getProperty("java.class.path" )); System.out.println("Operating System Name : " + System.getProperty("os.name" )); System.out.println("Operating System Architecture : " + System.getProperty("os.arch" )); System.out.println("Operating System Version : " + System.getProperty("os.version" )); System.out.println("File separator : " + System.getProperty("file.separator" )); System.out.println("Path separator : " + System.getProperty("path.separator" )); System.out.println("Line separator : " + System.getProperty("line.separator" )); System.out.println("User account name : " + System.getProperty("user.name" )); System.out.println("User home directory : " + System.getProperty("user.home" )); System.out.println("User current working directory : " + System.getProperty("user.dir" )); } }
运行
以本地系统为测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Java Version : 1.8.0_111 Java Vendor : Oracle Corporation Java vendor URL : http://java.oracle.com/ Java Installation Directory : D:\Java\jdk1.8.0_111\jre Java class version number : 52.0 Java classpath : D:\eclipseworklace\personworkplace\Devnp-System\target\classes;D:\Program Files\apache-maven-3.1.0\osrepo\mysql\mysql-connector-java\5.1.28\mysql-connector-java-5.1.28.jar;D:\Program Files\apache-maven-3.1.0\osrepo\org\slf4j\slf4j-api\1.7.9\slf4j-api-1.7.9.jar;D:\Program Files\apache-maven-3.1.0\osrepo\org\slf4j\slf4j-log4j12\1.7.9\slf4j-log4j12-1.7.9.jar;D:\Program Files\apache-maven-3.1.0\osrepo\log4j\log4j\1.2.17\log4j-1.2.17.jar Operating System Name : Windows 7 Operating System Architecture : amd64 Operating System Version : 6.1 File separator : \ Path separator : ; Line separator : User account name : duliu User home directory : C:\Users\duliu User current working directory : D:\eclipseworklace\personworkplace\Devnp-System
获取系统全部属性
我们可以通过如下方法,来一次性获取全部属性:
1 Properties properties = System.getProperties() ;
如果想打印出全部属性,可以使用:
1 System.getProperties().list(System.out);
或者使用:
1 2 3 4 5 6 7 8 9 Properties properties = System.getProperties() ;Enumeration enumeration = properties.propertyNames() ;while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement() ; System.out.println(key + " = " + properties.getProperty(key)); }
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE