The concept of environmental variables is not unfamiliar, is the operating system environment variables.
The system variable is the variable that the Java itself maintains. Acquired by means of System.getproperty.
For different operating systems, environment variables can be handled in a number of areas that are not uniform, such as: case-insensitive, and so on.
Java Get Environment variables
the way Java gets environment variables is simple:
System.getenv () Get 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 a Windows system, the printed value is the same as the environment variables seen from the "My Computer".
Java get and set system variables
the way Java gets environment variables is also simple:
System.getproperties () Get all the system variables
System.getproperty (key) to get 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 get the system variables, you can set the system variables that you need by System.setproperty (key, value).
Which system variables are set by default in Java:
- Java.version Java Runtime Environment version
- Java.vendor Java Runtime Environment Vendor
- Java.vendor.url Java Vendor's URL
- Java.home Java installation directory
- Java.vm.specification.version Java Virtual Machine specification version
- Java.vm.specification.vendor Java virtual Machine specification Vendor
- Java.vm.specification.name Java Virtual Machine Specification Name
- Java.vm.version Java Virtual Machine implementation version
- Java.vm.vendor Java virtual Machine implementation Provider
- 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 class Path
- Java.library.path the list of paths to search when loading libraries
- Java.io.tmpdir Default Temporary file path
- Java.compiler the name of the JIT compiler to use
- Java.ext.dirs the path of one or more extended directories
- Os.name the name of the operating system
- Architecture of the Os.arch operating system
- Os.version version of the operating system
- File.separator file Separator (in UNIX system is "/")
- Path.separator path Separator (in UNIX system is ":")
- Line.separator line delimiter (in UNIX system is "n")
- User.Name User's account name
- User.home User's home directory
- User.dir User's current working directory
Add
1. In. bat; . in cmd or. sh, some variables are set in the form
Like WebLogic's setdomainenv.cmd.
Set sun_java_home=c:\oracle\middleware\jdk160_21
The environment variables are set here
2. In the log4j configuration, the log file generation path is sometimes configured.
For example, ${log_dir}/logfile.log, where the Log_dir is replaced by a variable of the system attribute.
3. Look at the Java source code, through System.getproperties () the way to get system variables, there will be a security check
public static Properties GetProperties () {
SecurityManager sm = getSecurityManager ();
if (SM!= null) {
sm.checkpropertiesaccess ();
}
return props;
}
In a single Java application test, the SecurityManager in System is empty.
When the applet is running, it is combined with the. Policy this file to check permissions.
If you give an empty SecurityManager, 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, 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 produce more global effects because they are not only visible to Java child processes, but are visible to all child processes that define them. On different operating systems, their semantics are subtly different, for example, case-insensitive. 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 the use of environment variables (such as PATH).
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 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
--------------------------------------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