Java程序查询系统参数
系统参数就是系统中设定的一些参数,比如系统名称、JVM的版本等。Java的程序可以跨平台执行,可以通过这些系统参数的值目前执行的环境是怎样的,然后程序在执行的时候可以根据不同平台的特性,动态的做最佳的调整。下面代码可以取得系统参数。
package test;
import java.util.Enumeration;
import java.util.Properties;
public class Test {
public static void main(String args[]){
Properties p = System.getProperties();
Enumeration enume = p.propertyNames();
String propertyName = "";
String property = "";
while( enume.hasMoreElements()){
propertyName = (String)enume.nextElement();
property = p.getProperty(propertyName);
System.out.println("Property "+ propertyName +"' = '"+property+"'");
}
}
}
运行的结果是:
Property java.runtime.name' = 'Java(TM) SE Runtime Environment' Property sun.boot.library.path' = 'C:\Program Files\Java\jre7\bin' Property java.vm.version' = '23.25-b01' Property java.vm.vendor' = 'Oracle Corporation' Property java.vendor.url' = 'http://java.oracle.com/' Property path.separator' = ';' Property java.vm.name' = 'Java HotSpot(TM) Client VM' Property file.encoding.pkg' = 'sun.io' Property user.script' = '' Property user.country' = 'CN' Property sun.java.launcher' = 'SUN_STANDARD' Property sun.os.patch.level' = 'Service Pack 1' Property java.vm.specification.name' = 'Java Virtual Machine Specification' Property user.dir' = 'C:\Users\Administrator\workspace\Test' Property java.runtime.version' = '1.7.0_25-b17' Property java.awt.graphicsenv' = 'sun.awt.Win32GraphicsEnvironment' Property java.endorsed.dirs' = 'C:\Program Files\Java\jre7\lib\endorsed' Property os.arch' = 'x86' Property java.io.tmpdir' = 'C:\Users\ADMINI~1\AppData\Local\Temp\' Property line.separator' = ' ' Property java.vm.specification.vendor' = 'Oracle Corporation' Property user.variant' = '' Property os.name' = 'Windows 7' Property sun.jnu.encoding' = 'GBK' Property java.library.path' = 'C:\Program Files\Java\jre7\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;D:\app\Administrator\product\11.2.0\dbhome_1\bin;C:\Program Files\Java\jdk1.7.0_25;D:\Program Files\Java\jdk1.6.0_27;\bin;C:\Program Files\Java\jdk1.7.0_25;D:\Program Files\Java\jdk1.6.0_27;\jre\bin;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;D:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;D:\Program Files\Java\jdk1.6.0_27;\bin;D:\Program Files\Java\jdk1.6.0_27;\jre\bin;d:\Program Files\Windows7Master;D:\Program Files\MySQL\MySQL Utilities 1.3.4\;D:\Program Files\MATLAB\R2010b\runtime\win32;D:\Program Files\MATLAB\R2010b\bin;C:\Program Files\jEdit;D:\eclipse;;.' Property java.specification.name' = 'Java Platform API Specification' Property java.class.version' = '51.0' Property sun.management.compiler' = 'HotSpot Client Compiler' Property os.version' = '6.1' Propert