📜  如何在Java中获取系统属性和环境变量的值?

📅  最后修改于: 2022-05-13 01:54:22.890000             🧑  作者: Mango

如何在Java中获取系统属性和环境变量的值?

如何获取环境变量的值?

Java中的 System 类提供了一个名为System.getenv()的方法,可用于获取当前系统中设置的环境变量的值。

句法:

public static String getenv(String key);

where key is the Environment variable
whose values we want

下面的例子说明了如何使用 System.getenv() 来获取系统环境变量:

示例 1:获取特定环境变量的值

// Java program to get the value
// of a specific environment variable
// using System.getenv() method
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the value of
        // the TEMP environment variable
        System.out.println(System.getenv("TEMP"));
  
        // Get the value of
        // the OS environment variable
        System.out.println(System.getenv("OS"));
  
        // Get the value of
        // the JAVA_HOME environment variable
        System.out.println(System.getenv("JAVA_HOME"));
    }
}

输出:

示例 2:一次获取所有环境变量的值

// Java program to get the value
// of all environment variables at once
// using System.getenv() method
  
import java.util.Map;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the value of
        // all environment variables at once
        // and store it in Map
        Map env
            = System.getenv();
  
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

输出:

注意:输出将取决于您运行上述代码的系统。上面给出了一个示例输出

如何获取系统属性的值?

Java中的 System 类有两种用于读取系统属性的方法:

  • Java.lang.System.getProperty(String key):仅获取那些属性——您将使用键指定的值(与您想要的特定值相关联)。

    例子:

    // Java Program illustrating the working
    // of getProperty(String key) method
      
    import java.lang.*;
    import java.util.Properties;
      
    public class NewClass {
        public static void main(String[] args)
        {
            // Printing Name of the system property
            System.out.println("user.dir: "
                               + System.getProperty(
                                     "user.dir"));
      
            // Fetches the property set with 'home' key
            System.out.println("home: "
                               + System.getProperty(
                                     "home"));
            // Resulting in Null as no property is present
      
            // Printing 'name of Operating System'
            System.out.println("os.name: "
                               + System.getProperty(
                                     "os.name"));
      
            // Printing 'JAVA Runtime version'
            System.out.println("version: "
                               + System.getProperty(
                                     "java.runtime.version"));
      
            // Printing 'name' property
            System.out.println("name: "
                               + System.getProperty(
                                     "name"));
            // Resulting in Null as no property is present
        }
    }
    

    输出:

    user.dir: /tmp/hsperfdata_bot
    home: null
    os.name: Linux
    version: 1.8.0_101-b13
    name: null
    
  • Java.lang.System.getProperty(String key, String definition):帮助您创建自己想要的键值集。

    例子:

    // Java Program illustrating the working of
    // getProperty(String key, String definition) method
      
    import java.lang.*;
    import java.util.Properties;
      
    public class NewClass {
        public static void main(String[] args)
        {
      
            // Here key = "Hello" and
            // System Property = "Geeks"
            System.out.println("Hello property : "
                               + System.getProperty(
                                     "Hello", "Geeks"));
      
            // Here key = "Geek" and
            // System Property = "For Geeks"
            System.out.println("System-property :"
                               + System.getProperty(
                                     "System", "For Geeks"));
      
            // Here key = "Property" and
            // System Property = null
            System.out.println("Property-property :"
                               + System.getProperty(
                                     "Property"));
        }
    }
    

    输出:

    Hello key property : Geeks
    System key property :For Geeks
    Property key property :null
    
  • Java.lang.System.getProperties():获取所有属性——系统上的 JVM 从操作系统获取的值。

    例子:

    // Java Program illustrating the working of
    // getProperties() method
      
    public class GFG {
        public static void main(String[] args)
        {
      
            System.out.println(System.getProperties())
        }
    }
    

    输出: