system getenv_Java System类getenv()方法及示例

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SystemJava中的一种包含一些系统级别的方法和变量的实用。它提供了一些与系统相关的方法和属性,例如与标准输入/输出流相关的方法,以及与系统相关的属性,例如当前时间、系统版本等。 以下是System的一些常见方法和其作用: 1. public static void exit(int status): 退出Java虚拟机,将status作为退出状态值。 2. public static long currentTimeMillis(): 返回当前时间(以毫秒为单位)。 3. public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length): 复制数组,将源数组src中的元素复制到目标数组dest中。 4. public static String getProperty(String key): 获取指定系统属性的值。 5. public static String getenv(String name): 获取指定环境变量的值。 6. public static void gc(): 运行Java虚拟机的垃圾回收器。 7. public static PrintStream err: 标准错误输出流。 8. public static PrintStream out: 标准输出流。 下面是一个示例程序,演示了如何使用System输出当前时间和Java版本: ```java public class SystemExample { public static void main(String[] args) { // 输出当前时间 long currentTime = System.currentTimeMillis(); System.out.println("当前时间:" + currentTime); // 输出Java版本 String javaVersion = System.getProperty("java.version"); System.out.println("Java版本:" + javaVersion); } } ``` 该程序输出的结果似于: ``` 当前时间:1629751734510 Java版本:1.8.0_301 ``` 可以看到,该程序使用了System的currentTimeMillis()方法和getProperty()方法来输出当前时间和Java版本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值