JVM系统属性(System.getProperty())和环境变量(System.getenv());
// 系统属性
String javaVersion = System.getProperty("java.version");
String javaVendor = System.getProperty("java.vendor");
String javaVendorUrl = System.getProperty("java.vendor.url");
String javaHome = System.getProperty("java.home");
String javaVmSpecificationVersion = System.getProperty("java.vm.specification.version");
String javaVmSpecificationVendor = System.getProperty("java.vm.specification.vendor");
String javaVmSpecificationName = System.getProperty("java.vm.specification.name");
String javaVmVersion = System.getProperty("java.vm.version");
String javaVmVendor = System.getProperty("java.vm.vendor");
String javaVmName = System.getProperty("java.vm.name");
String javaSpecificationVersion = System.getProperty("java.specification.version");
String javaSpecificationVendor = System.getProperty("java.specification.vendor");
String javaSpecificationName = System.getProperty("java.specification.name");
String javaClassVersion = System.getProperty("java.class.version");
String javaClassPath = System.getProperty("java.class.path");
String javaLibraryPath = System.getProperty("java.library.path");
String javaIoTmpdir = System.getProperty("java.io.tmpdir");
String javaCompiler = System.getProperty("java.compiler");
String javaExtDirs = System.getProperty("java.ext.dirs");
String osName = System.getProperty("os.name");
String osArch = System.getProperty("os.arch");
String osVersion = System.getProperty("os.version");
String fileSeparator = System.getProperty("file.separator");
String pathSeparator = System.getProperty("path.separator");
String lineSeparator = System.getProperty("line.separator");
String userName = System.getProperty("user.name");
String userHome = System.getProperty("user.home");
String userDir = System.getProperty("user.dir");
注解:
java.version | Java 运行时版本 |
java.home | Java 的安装目录 |
java.class.version | Java 类格式的版本号 |
java.class.path | Java 类的查找路径 |
java.io.tmpdir | 默认的临时目录 |
java.compiler | Java 所使用的及时编译器 |
java.ext.dirs | Java 扩展包的目录 |
os.name | 操作系统的名称 |
os.arch | 操作系统的体系结构 |
os.version | 操作系统的版本 |
file.separator | 文件分隔符(Unix 下为'/') |
path.separator | 路径分隔符(Unix 下为':') |
line.separator | 换行符(Unix 下为'/n') |
user.name | 用户帐号名 |
user.home | 用户目录 |
user.dir | 用户当前的工作目录
|
- 取得操作系统的环境变量
注意,这次是获取操作系统的环境变量,而不是获取JVM相关的一些变量。
也许是为了营造JVM就是操作系统平台的气氛,抑或是为了强调 Java的平台无关性,不知几时起Java已经把System.getenv(String)函数废弃了。所以一般来说Java只能获取它自己定义的一些 变量,而无法与操作系统的环境变量交互,只能在运行靠java的“-D”参数来设置要传递给它的一些变量。
所以唯一的办法只能先判断操作系统,然后用操作系统的命令来调出环境变量列表,设法获得该输出列表。
- 代码实现
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;public class SystemValue {
/**
* 使用System获取系统相关的值
*/
public static void getSystemProperties() {
Properties pp = System.getProperties();
System.out.println("System's Properties:");
System.out.println();
java.util.Enumeration en = pp.propertyNames();
while (en.hasMoreElements()) {
String nextE = (String) en.nextElement();
System.out.print(nextE + "=" + pp.getProperty(nextE));
System.out.println();
}
}public static void getCustomProperties(String key) {
Map map = getEnv();
System.out.println(key + "=" + map.get(key));
}public static Map getEnv() {
Map map = new HashMap();
Process p = null;
Runtime r = Runtime.getRuntime();
String OS = System.getProperty("os.name").toLowerCase();
System.out.println("OS="+OS);
try {
if (OS.indexOf("windows 9") > -1) {
p = r.exec("command.com /c set");
} else if ((OS.indexOf("nt") > -1)
|| (OS.indexOf("windows 20") > -1)
|| (OS.indexOf("windows xp") > -1)) {
p = r.exec("cmd.exe /c set");
} else {
// Unix
p = r.exec("env");
}
BufferedReader br = new BufferedReader(new InputStreamReader(p
.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
String[] str = line.split("=");
map.put(str[0], str[1]);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return map;
}public static void main(String[] args) {
// getSystemProperties();
getCustomProperties("JAVA_HOME");
}
}