Home  >  Article  >  Java  >  java gets system variables (environment variables and setting variables)

java gets system variables (environment variables and setting variables)

高洛峰
高洛峰Original
2016-12-17 13:07:011421browse

Foreword

The concept of environment variables is not unfamiliar, it is the environment variable of the operating system.

System variables are variables maintained by Java itself. Obtained through System.getProperty.

For different operating systems, there may be some inconsistencies in the processing of environment variables, such as: case insensitivity, etc.

Java gets environment variables

The way to get environment variables in Java is very simple:

System.getEnv() Gets all environment variables

System.getEnv(key) Gets the value of an environment variable

[java] view plain copy
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 variable seen in "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 a certain system variable In addition to obtaining the value of

[java] view plain copy
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 variables, you can also set the system variables you need through System.setProperty(key, value).

What system variables are set by java by default:

java.version Java runtime environment version
java.vendor Java runtime environment vendor
java.vendor.url Java vendor 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 supplier
java.vm.name Java virtual machine implementation name
java.specification.version Java runtime environment specification version
java.specification.vendor Java runtime environment specification supplier
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 Path list searched when loading the library
java.io. tmpdir The default temporary file path
java.compiler The name of the JIT compiler to be used
java.ext.dirs The path to one or more extension directories
os.name The name of the operating system
os.arch The architecture of the operating system
os .version operating system version
file.separator file separator (in UNIX system is "/")
path.separator path separator (in UNIX system is ":")
line.separator line separator (in UNIX system In the system, it is "/n")
user.name The user's account name
user.home The user's home directory
user.dir The user's current working directory


Supplementary

1. In .bat; .cmd Or .sh will set some variables by set,

For example, weblogic's setDomainEnv.cmd

set SUN_JAVA_HOME=C:OracleMiddlewarejdk160_21

The environment variables are set here

2. In the configuration of log4j, sometimes Configure the generation path of the log file.

For example, ${LOG_DIR}/logfile.log, LOG_DIR here is replaced by the system attribute variable.

3. Take a look at the java source code. When obtaining system variables through System.getProperties(), there will be a security check

[java] view plain copy
   public static Properties getProperties() {  
SecurityManager sm = getSecurityManager();  
       if (sm != null) {  
    sm.checkPropertiesAccess();  
}  
  
return props;  
   }


When testing a single Java application, the SecurityManager in System is empty.

When the Applet is running, it will check the permissions in combination with the .policy file.

If you give an empty SecurityManager, you will find that a permission exception will be thrown.

[java] view plain copy
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();  
}



For more java related articles on obtaining system variables (environment variables and setting variables), please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn