Explain how Java gets environment variables and system properties

  • 2020-05-09 18:42:20
  • OfStack

The concept of environment variables is familiar, that is, the environment variables of the operating system.
System variables are the variables that java itself maintains. Get it by System.getProperty.
For different operating systems, the handling of environment variables may be inconsistent, such as case-insensitive, etc.

Java gets environment variables
The way Java gets environment variables is simple:
System.getEnv ()   gets all the 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 an windows system, the printed value is one of the environment variables seen from "my computer".

Java gets and sets system variables
The way Java gets environment variables is also simple:
System.getProperties ()   gets all the system variables
System.getProperty (key)   gets the value of some 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 the system variables of  , you can also set the system variables you need by System.setProperty (key, value)  .

By default, which system variables are set by java:

java. version Java runtime environment version java. vendor Java runtime environment provider java.vendor.url Java supplier URL java.home Java installation directory java. vm. specification. version Java virtual machine specification version java. vm. specification. vendor Java virtual machine specification supplier java. vm. specification. name Java virtual machine specification name java. vm. version Java virtual machine implementation version java.vm.vendor Java virtual machine implementation vendor java.vm.name Java virtual machine implementation name java.specification.version Java runtime environment specification version java.specification.vendor Java runtime environment specification vendor java.specification.name Java runtime environment specification name java.class.version Java class format version number java. class. path Java classpath java.library.path the list of paths searched when loading the library java.io.tmpdir default temporary file path The name of the JIT compiler to be used by java.compiler java.ext.dirs the path to one or more extended directories Name of the os.name operating system os.arch operating system architecture Version of the os.version operating system file.separator file separator ("/" in UNIX) path.separator path separator (":" in UNIX system) line.separator line separator ("/n" in the UNIX system) user.name user's account name user.home user's home directory user.dir user's current working directory

supplement
1. At.  .cmd or  .sh will set some variables by means of set,
weblogic, setDomainEnv.cmd
set SUN_JAVA_HOME=C:\Oracle\Middleware\jdk160_21
The environment variables are set here
2. In the configuration of log4j, the generation path of log file is sometimes configured.
For example, ${LOG_DIR}/ logfile.log, where LOG_DIR is replaced by a variable of system properties.
3. Take a look at the java source code. There will be a security check when obtaining system variables through System.getProperties ()
 


  public static Properties getProperties() { 
SecurityManager sm = getSecurityManager(); 
    if (sm != null) { 
  sm.checkPropertiesAccess(); 
} 
 
return props; 
  } 

In a single Java application   test, SecurityManager in System is empty.
When Applet runs, permissions are checked in conjunction with the.policy file.

If you give an empty SecurityManager you will find it and you will throw a permission exception.
 


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 ()
Conceptually, both system properties and environment variables are mappings between names and values. Both mechanisms can be used to pass user-defined information to the Java process. Environment variables have more global effects because they are visible not only to the Java child processes, but also to all the child processes of the processes that define them. On different operating systems, they have slightly different semantics, such as case-insensitivity. For these reasons, environment variables are more likely to have unexpected side effects. It is best to use system properties where possible. Environment variables should be used when global effects are required, or when external system interfaces require them (such as PATH). The code is as follows:

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);
       }
    }
 
Enter as follows:

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
The following is the output of 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

Related articles: