詳解Java獲得情況變量及體系屬性的辦法。本站提示廣大學習愛好者:(詳解Java獲得情況變量及體系屬性的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解Java獲得情況變量及體系屬性的辦法正文
情況變量這個概念不生疏, 就是操作體系的情況變量。
體系變量就是java自己保護的變量。 經由過程 System.getProperty 的方法獲得。
關於分歧的操作體系來講, 情況變量的處置能夠會有一些不同一的處所, 好比說: 不辨別年夜小寫 等等。
Java 獲得情況變量
Java 獲得情況變量的方法很簡略:
System.getEnv() 獲得一切的情況變量
System.getEnv(key) 獲得某個情況變量的值
Map map = System.getenv(); Iterator it = map.entrySet().iterator(); while(it.hasNext()) { Entry entry = (Entry)it.next(); System.out.print(entry.getKey()+"="); System.out.println(entry.getValue()); }
假如是windows 體系, 打印出來的值經由過程從 “我的電腦” 裡看到的情況變量是一樣的。
Java 獲得和設置體系變量
Java 獲得情況變量的方法也很簡略:
System.getProperties() 獲得一切的體系變量
System.getProperty(key) 獲得某個體系變量的值
Properties properties = System.getProperties(); Iterator it = properties.entrySet().iterator(); while(it.hasNext()) { Entry entry = (Entry)it.next(); System.out.print(entry.getKey()+"="); System.out.println(entry.getValue()); }
體系變量除可以獲得以外, 還可以經由過程System.setProperty(key, value) 的方法設置本身須要的體系變量。
默許情形下, java 設置了哪些體系變量:
彌補
1 . 在.bat ; .cmd 或 .sh 中會經由過程 set 的方法設置一些變量,
好比weblogic 的 setDomainEnv.cmd
set SUN_JAVA_HOME=C:\Oracle\Middleware\jdk160_21
這裡設置的是情況變量
2. 在log4j 的設置裝備擺設中, 有時會設置裝備擺設log file 的發生途徑。
好比 ${LOG_DIR}/logfile.log, 這裡的LOG_DIR 是被體系屬性的變量調換的。
3. 看一下java 源碼, 經由過程 System.getProperties() 的方法獲得體系變量時, 會有一個平安的檢討
public static Properties getProperties() { SecurityManager sm = getSecurityManager(); if (sm != null) { sm.checkPropertiesAccess(); } return props; }
在 單個Java application 測試時, System 裡的SecurityManager 是空的。
在Applet 運轉時, 會聯合 .policy 這個文件來檢討權限。
假如給一個空的SecurityManager 會發明就會拋出權限異常。
public static void main(String[] args) { // TODO Auto-generated method stub System.setSecurityManager(new SecurityManager()); //SecurityManager sm = System.getSecurityManager(); //System.out.println(sm); System.getSecurityManager().checkPropertiesAccess(); }System.getEnv()和System.getProperties()的差別
public static void main(String [] args) { Map m = System.getenv(); for ( Iterator it = m.keySet().iterator(); it.hasNext(); ) { String key = (String ) it.next(); String value = (String ) m.get(key); System.out.println(key +":" +value); } System.out.println( "--------------------------------------" ); Properties p = System.getProperties(); for ( Iterator it = p.keySet().iterator(); it.hasNext(); ) { String key = (String ) it.next(); String value = (String ) p.get(key); System.out.println(key +":" +value); } }
ANT_HOME:D:/program/devel/ant PROCESSOR_ARCHITECTURE:x86 LOGONSERVER://RJ-WEIJIANJUN HOMEDRIVE:C: CATALINA_HOME:D:/program/server/Tomcat5.5 DXSDK_DIR:d:/Program Files/Microsoft DirectX SDK (August 2008)/ VS80COMNTOOLS:C:/Program Files/Microsoft Visual Studio 8/Common7/Tools/ SESSIONNAME:Console HOMEPATH:/Documents and Settings/Administrator TMP:C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp windir:C:/WINDOWS PROCESSOR_IDENTIFIER:x86 Family 6 Model 15 Stepping 13, GenuineIntel VS90COMNTOOLS:e:/Program Files/Microsoft Visual Studio 9.0/Common7/Tools/ SystemDrive:C: USERPROFILE:C:/Documents and Settings/Administrator PATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH CommonProgramFiles:C:/Program Files/Common Files NUMBER_OF_PROCESSORS:2 ComSpec:C:/WINDOWS/system32/cmd.exe COMPUTERNAME:RJ-WEIJIANJUN OS:Windows_NT USERNAME:Administrator CLIENTNAME:Console TEMP:C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp USERDOMAIN:RJ-WEIJIANJUN ALLUSERSPROFILE:C:/Documents and Settings/All Users lib:C:/Program Files/SQLXML 4.0/bin/ PROCESSOR_LEVEL:6 SystemRoot:C:/WINDOWS ClusterLog:C:/WINDOWS/Cluster/cluster.log APPDATA:C:/Documents and Settings/Administrator/Application Data Path:C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;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/devel/flex_sdk2/bin;D:/program/devel/ant/bin;C:/Program Files/Java/jdk1.6.0_07/bin;%JONAS_ROOT%/bin/nt;d:/program/devel/ant/bin JAVA_HOME:C:/Program Files/Java/jdk1.6.0_07 FP_NO_HOST_CHECK:NO PROCESSOR_REVISION:0f0d ProgramFiles:C:/Program Files以下是property的輸入:
-------------------------------------- java.runtime.name:Java(TM) 2 Runtime Environment, Standard Edition sun.boot.library.path:D:/Program Files/MyEclipse 6.5/jre/bin java.vm.version:1.5.0_11-b03 java.vm.vendor:Sun Microsystems Inc. java.vendor.url:http://java.sun.com/ path.separator:; java.vm.name:Java HotSpot(TM) Client VM file.encoding.pkg:sun.io sun.java.launcher:SUN_STANDARD user.country:CN sun.os.patch.level:Service Pack 2 java.vm.specification.name:Java Virtual Machine Specification user.dir:D:/dev/eclipse/mye65/workspace/jmx java.runtime.version:1.5.0_11-b03 java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironment java.endorsed.dirs:D:/Program Files/MyEclipse 6.5/jre/lib/endorsed os.arch:x86 java.io.tmpdir:C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ line.separator: java.vm.specification.vendor:Sun Microsystems Inc. user.variant: os.name:Windows 2003 sun.jnu.encoding:GBK java.library.path:D:/Program Files/MyEclipse 6.5/jre/bin;.;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;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/devel/flex_sdk2/bin;D:/program/devel/ant/bin;C:/Program Files/Java/jdk1.6.0_07/bin;%JONAS_ROOT%/bin/nt;d:/program/devel/ant/bin java.specification.name:Java Platform API Specification java.class.version:49.0 sun.management.compiler:HotSpot Client Compiler os.version:5.2 user.home:C:/Documents and Settings/Administrator user.timezone:Asia/Shanghai java.awt.printerjob:sun.awt.windows.WPrinterJob file.encoding:GBK java.specification.version:1.5 java.class.path:D:/dev/eclipse/mye65/workspace/jmx/bin;D:/program/lib/jmx/jmxtools.jar;D:/program/lib/log/commons-logging-1.1.1.jar;D:/program/lib/log/log4j-1.2.15.jar;D:/program/lib/registry/registry.jar user.name:Administrator java.vm.specification.version:1.0 java.home:D:/Program Files/MyEclipse 6.5/jre sun.arch.data.model:32 user.language:zh java.specification.vendor:Sun Microsystems Inc. awt.toolkit:sun.awt.windows.WToolkit java.vm.info:mixed mode java.version:1.5.0_11 java.ext.dirs:D:/Program Files/MyEclipse 6.5/jre/lib/ext sun.boot.class.path:D:/Program Files/MyEclipse 6.5/jre/lib/rt.jar;D:/Program Files/MyEclipse 6.5/jre/lib/i18n.jar;D:/Program Files/MyEclipse 6.5/jre/lib/sunrsasign.jar;D:/Program Files/MyEclipse 6.5/jre/lib/jsse.jar;D:/Program Files/MyEclipse 6.5/jre/lib/jce.jar;D:/Program Files/MyEclipse 6.5/jre/lib/charsets.jar;D:/Program Files/MyEclipse 6.5/jre/classes java.vendor:Sun Microsystems Inc. file.separator:/ java.vendor.url.bug:http://java.sun.com/cgi-bin/bugreport.cgi sun.io.unicode.encoding:UnicodeLittle sun.cpu.endian:little sun.desktop:windows sun.cpu.isalist:pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86