本例以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) {
// TODO Auto-generated method stub
System.out.println("Java Version : " + System.getProperty("java.version")); //Java 版本
System.out.println("Java Vendor : " + System.getProperty("java.vendor")); //Java 供应商
System.out.println("Java vendor URL : " + System.getProperty("java.vendor.url")); //Java 供应商URL地址

System.out.println("Java Installation Directory : " + System.getProperty("java.home")); //Java 安装路径
System.out.println("Java class version number : " + System.getProperty("java.class.version")); //Java 版本号
System.out.println("Java classpath : " + System.getProperty("java.class.path")); //Java 类路径

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));
}