首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JVM系统相干

2012-09-10 
JVM系统相关JVM系统属性(System.getProperty())和环境变量(System.getenv()) // 系统属性? String javaVe

JVM系统相关

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.versionJava 运行时版本java.homeJava 的安装目录java.class.versionJava 类格式的版本号java.class.pathJava 类的查找路径java.io.tmpdir默认的临时目录java.compilerJava 所使用的及时编译器java.ext.dirsJava 扩展包的目录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");
?}
}

热点排行