Preface
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
Supplements
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 ();
}