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'
Property user.home' = 'C:\Users\Administrator'
Property user.timezone' = ''
Property java.awt.printerjob' = 'sun.awt.windows.WPrinterJob'
Property file.encoding' = 'GBK'
Property java.specification.version' = '1.7'
Property user.name' = 'Administrator'
Property java.class.path' = 'C:\Users\Administrator\workspace\Test\bin'
Property java.vm.specification.version' = '1.7'
Property sun.arch.data.model' = '32'
Property java.home' = 'C:\Program Files\Java\jre7'
Property sun.java.command' = 'test.Test'
Property java.specification.vendor' = 'Oracle Corporation'
Property user.language' = 'zh'
Property awt.toolkit' = 'sun.awt.windows.WToolkit'
Property java.vm.info' = 'mixed mode, sharing'
Property java.version' = '1.7.0_25'
Property java.ext.dirs' = 'C:\Program Files\Java\jre7\lib\ext;C:\windows\Sun\Java\lib\ext'
Property sun.boot.class.path' = 'C:\Program Files\Java\jre7\lib\resources.jar;C:\Program Files\Java\jre7\lib\rt.jar;C:\Program Files\Java\jre7\lib\sunrsasign.jar;C:\Program Files\Java\jre7\lib\jsse.jar;C:\Program Files\Java\jre7\lib\jce.jar;C:\Program Files\Java\jre7\lib\charsets.jar;C:\Program Files\Java\jre7\lib\jfr.jar;C:\Program Files\Java\jre7\classes'
Property java.vendor' = 'Oracle Corporation'
Property file.separator' = '\'
Property java.vendor.url.bug' = 'http://bugreport.sun.com/bugreport/'
Property sun.cpu.endian' = 'little'
Property sun.io.unicode.encoding' = 'UnicodeLittle'
Property sun.desktop' = 'windows'
Property sun.cpu.isalist' = 'pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86'
当然,不同的平台所得到的结果会不一样。