本文共 2631 字,大约阅读时间需要 8 分钟。
system getenv
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.javaE:\Programs>java GetenvMethodsystem_key= PATHsystem_key_value= /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-10-jdk/binsystem_key= SHELLsystem_key_value= /bin/bashsystem_key= HOSTNAMEsystem_key_value= jdoodlesystem_key= JAVA_OPTSsystem_key_value= --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMEDsystem_key= PWDsystem_key_value= /homesystem_key= SHLVLsystem_key_value= 1system_key= HOMEsystem_key_value= /rootsystem_key= _system_key_value= /usr/bin/time
翻译自:
system getenv
转载地址:http://fnxzd.baihongyu.com/