system getenv
系統類getenv()方法 (System class getenv() method)
getenv() method is available in java.lang package.
getenv()方法在java.lang包中可用。
getenv() method is used to return an unmodifiable Map of the current environment variable in key-value pairs.
getenv()方法用于返回鍵值對中當前環境變量的不可修改Map。
We will see what is environment variable ? An environment variable is a system dependent external named value.
我們將看到什么是環境變量? 環境變量是系統相關的外部命名值。
We should go for environment variable when a system interface needed an environment variable (such as PATH).
當系統接口需要環境變量(例如PATH)時,我們應該選擇環境變量。
When we assign the value of the environment variable then we don’t need to assign the value of the environment variable again while working.
當我們分配環境變量的值時,我們就不需要在工作時再次分配環境變量的值。
getenv() method is a static method so it is accessible with the class name too.
getenv()方法是靜態方法,因此也可以使用類名進行訪問。
getenv() method method may throw an exception at the time of retrieving the environment variable:
getenv()方法 method可能在檢索環境變量時引發異常:
SecurityException: If a particular method checkPermission() does not allow access to the process environment when the security manager exists.
SecurityException :如果存在安全管理器時,如果特定方法checkPermission()不允許訪問流程環境。
Syntax:
句法:
public static Map getenv ();
Parameter(s):
參數:
This method does not accept any parameter.
此方法不接受任何參數。
Return value:
返回值:
The return type of this method is Map, it returns the environment variable with the desired value.
此方法的返回類型為Map ,它返回具有所需值的環境變量。
Example:
例:
// Java program to demonstrate the example of
// getenv() method of System Class.
import java.util.*;
import java.lang.*;
public class GetenvMethod {
public static void main(String[] args) {
Map < String, String > map = System.getenv();
// By using keyset() method is used to iterate
Set < String > system_keys = map.keySet();
for (String keys: system_keys) {
System.out.println("system_key= " + keys);
String values = map.get(keys);
System.out.println("system_key_value= " + values);
}
}
}
Output
輸出量
E:\Programs>javac GetenvMethod.java
E:\Programs>java GetenvMethod
system_key= PATH
system_key_value= /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-10-jdk/bin
system_key= SHELL
system_key_value= /bin/bash
system_key= HOSTNAME
system_key_value= jdoodle
system_key= JAVA_OPTS
system_key_value= --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED
system_key= PWD
system_key_value= /home
system_key= SHLVL
system_key_value= 1
system_key= HOME
system_key_value= /root
system_key= _
system_key_value= /usr/bin/time
翻譯自: https://www.includehelp.com/java/system-class-getenv-method-with-example.aspx
system getenv