The concept of environment variables is not unfamiliar with it, it is the environment variables of the operating system.
System variables are variables maintained by Java itself. Get it through System.getProperty.
For different operating systems, the processing of environment variables may be inconsistent, such as case insensitive, etc.
Java Get environment variables
Java's way of getting environment variables is very simple:
System.getEnv() gets all environment variables
System.getEnv(key) Gets the value of an environment variable
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()); }If it is a Windows system, the printed value is the same as the environment variables seen from "My Computer".
Java gets and sets system variables
Java's way of getting environment variables is also very simple:
System.getProperties() Gets all system variables
System.getProperty(key) Gets the value of a system variable
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()); }In addition to being able to obtain system variables, you can also set the system variables you need through System.setProperty(key, value).
By default, what system variables are set in java:
Replenish
1. In .bat; .cmd or .sh, some variables will be set by set.
For example, weblogic's setDomainEnv.cmd
set SUN_JAVA_HOME=C:/Oracle/Middleware/jdk160_21
The environment variable is set here
2. In the log4j configuration, the generation path of the log file is sometimes configured.
For example, ${LOG_DIR}/logfile.log, the LOG_DIR here is replaced by a variable of the system attribute.
3. Take a look at the java source code. When you obtain system variables through System.getProperties(), there will be a safe check.
public static Properties getProperties() { SecurityManager sm = getSecurityManager(); if (sm != null) { sm.checkPropertiesAccess(); } return props; } The SecurityManager in the System is empty during a single Java application test.
When Applet runs, the permissions are checked in combination with the .policy file.
If you give an empty SecurityManager, you will find that a permission exception will be thrown.
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(); } The difference between System.getEnv() and 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/antPROCESSOR_ARCHITECTURE:x86LOGONSERVER://RJ-WEIJIANJUNHOMEDRIVE:C:CATALINA_HOME:D:/program/server/Tomcat5.5DXSDK_DIR:d:/Program Files/Microsoft DirectX SDK (August 2008)/VS80COMNTOOLS:C:/Program Files/Microsoft Visual Studio 8/Common7/Tools/SESSIONNAME:ConsoleHOMEPATH:/Documents and Settings/AdministratorTMP:C:/DOCUME~1/ADMINI~1/LOCALS~1/Tempwindir:C:/WINDOWSPROCESSOR_IDENTIFIER:x86 Family 6 Model 15 Stepping 13, GenuineIntelVS90COMNTOOLS:e:/Program Files/Microsoft Visual Studio 9.0/Common7/Tools/SystemDrive:C:USERPROFILE:C:/Documents and Settings/AdministratorPATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSHCommonProgramFiles:C:/Program Files/Common FilesNUMBER_OF_PROCESSORS:2ComSpec:C:/WINDOWS/system32/cmd.exeCOMPUTERNAME:RJ-WEIJIANJUNOS:Windows_NTUSERNAME:AdministratorCLIENTNAME:ConsoleTEMP:C:/DOCUME~1/ADMINI~1/LOCALS~1/TempUSERDOMAIN:RJ-WEIJIANJUNALLUSERSPROFILE:C:/Documents and Settings/All Userslib:C:/Program Files/SQLXML 4.0/bin/PROCESSOR_LEVEL:6SystemRoot:C:/WINDOWSClusterLog:C:/WINDOWS/Cluster/cluster.logAPPDATA:C:/Documents and Settings/Administrator/Application DataPath: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/binJAVA_HOME:C:/Program Files/Java/jdk1.6.0_07FP_NO_HOST_CHECK:NOPROCESSOR_REVISION:0f0dProgramFiles:C:/Program FilesHere is the output of the property:
--------------------------------------java.runtime.name:Java(TM) 2 Runtime Environment, Standard Editionsun.boot.library.path:D:/Program Files/MyEclipse 6.5/jre/binjava.vm.version:1.5.0_11-b03java.vm.vendor:Sun Microsystems Inc.java.vendor.url:http://java.sun.com/path.separator:;java.vm.name:Java HotSpot(TM) Client VMfile.encoding.pkg:sun.iosun.java.launcher:SUN_STANDARDuser.country:CNsun.os.patch.level:Service Pack 2java.vm.specification.name:Java Virtual Machine Specificationuser.dir:D:/dev/eclipse/mye65/workspace/jmxjava.runtime.version:1.5.0_11-b03java.awt.graphicsenv:sun.awt.Win32GraphicsEnvironmentjava.endorsed.dirs:D:/Program Files/MyEclipse 6.5/jre/lib/endorsedos.arch:x86java.io.tmpdir:C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/line.separator: java.vm.specification.vendor:Sun Microsystems Inc.user.variant:os.name:Windows 2003sun.jnu.encoding:GBKjava.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/;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/binjava.specification.name:Java Platform API Specificationjava.class.version:49.0sun.management.compiler:HotSpot Client Compileros.version:5.2user.home:C:/Documents and Settings/Administratoruser.timezone:Asia/Shanghaijava.awt.printerjob:sun.awt.windows.WPrinterJobfile.encoding:GBKjava.specification.version:1.5java.class.path:D:/dev/eclipse/mye65/workspace/jmx/bin;D:/program/li b/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.jaruser.name:Administratorjava.vm.specification.version:1.0java.home:D:/Program Files/MyEclipse 6.5/jresun.arch.data.model:32user.language:zhjava.specification.vendor:Sun Microsystems Inc.awt.toolkit:sun.awt.windows.WToolkitjava.vm.info:mixed modejava.version:1.5.0_11java.ext.dirs:D:/Program Files/MyEclipse 6.5/jre/lib/extsun.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/sunrasign.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/lib/charsets.jar;D:/Program Files/MyEclipse 6.5/jre/classesjava.vendor:Sun Microsystems Inc.file.separator:/java.vendor.url.bug:http://java.sun.com/cgi-bin/bugreport.cgisun.io.unicode.encoding:UnicodeLittlesun.cpu.endian:littlesun.desktop:windowsun.cpu.isalist:pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86