How to get the value of System Property and Environment Variable in Java?


Acquiring values of system properties and environment variables plays a pivotal role in accessing critical configuration information within applications written using Java. System properties encompass various elements of a system, such as its Java installation directory or version number or user preferences. Environment variables, on the other hand, are dynamic values set either by an operating system or user that impact application performance.

By accessing these properties and variables, you gain invaluable insights into the system configuration that enable you to adapt your code accordingly for greater compatibility, customization and seamless integration into its surrounding environment.

Environment Variable

Java environment variables are system-specific variables used to store information regarding the runtime environment. They consist of key-value pairs which are accessible by Java programs running within their environment. To work with environment variables, take advantage of System.getenv() which returns a Map with all environment variables. By passing in an environment variable name as a parameter to getenv() you can retrieve its associated value.

Use of entrySet() method enables accessing of environment variables by iteration over every key-value pair contained within Map. Environment variables are useful tools for many tasks including configuring application settings, accessing system properties or adapting program behavior based on runtime environment conditions.

Approaches

Java provides several methods for retrieving system properties and environment variables, but we will focus on two of the more frequently employed approaches here.

  • Using System.getProperty()

  • Using System.getenv()

Approach 1: Using System.getProperty()

Java offers the System.getProperty() method for retrieving values of system properties and environment variables. To retrieve system property values, provide its key as a parameter while to access environment variable values prefixed with "env." Both calls return values in string format so you can easily gain access to important runtime environment information like Java version, operating system version or any system-specific configuration details.

Environment variables provide users or system administrators with custom settings they can set at run time, making accessing and using this data extremely powerful for configuration of Java apps, customizing behavior or extracting specific details about their execution environment. By taking advantage of System.getProperty() you can gain access and leverage these values in order to change or customize any aspect of behavior within an app or extract specific details relevant to its execution environment.

Algorithm

How to determine the value of system properties:

  • Provide the key of the desired system property as the input parameter to System.getProperty().

  • Set the returned value as the parameter value and assign it directly into a variable for further use.

How to read out an environment variable value:

  • Add an "env." prefix before typing out the key of an environment variable you wish to access.

  • Provide the modified key as a parameter to System.getProperty().

Assign the returned value to a variable for later use in your Java program; such as for configuration or customization purposes or accessing system-specific data.

Note: Be certain that the desired system property or environment variable exists and can be reached from within your runtime environment.

Program

public class SystemPropertyExample {
   public static void main(String[] args) {
      // Retrieving the value of the "os.name" system property
      String osName = System.getProperty("os.name");
        
      // Displaying the retrieved value
      System.out.println("Operating System: " + osName);
   }
}

Output

Operating System: Linux

Approach 2: Using System.getenv()

Java's System.getenv() method makes it possible for you to get information about system properties and environment variables quickly and conveniently. This method returns a Map which contains all key-value pairs as a collection. By accessing this map, you can retrieve the value of an environment variable simply by providing its name as key. Environment variables contain system-related data that may help with configuration or customizing an operating system while giving access to system properties.

The System.getenv() method makes accessing environment variables simple and provides valuable insight into its runtime environment. By making use of its returned map and desired key, this method enables accessing system properties and environment variables within Java programs for various uses.

Algorithm

  • Use System.getenv() to retrieve environment variables.

  • Store environment variables using a Map data structure. Each key-value pair represents both its name and value in this way.

  • To determine the value of a specific environment variable:

    • Provide the name of an environment variable as key for this Map.

    • Retrieve the value associated with each key and assign it to one or more variables for future use.

  • Use the value retrieved in your Java program as needed - for configuration or customization purposes or accessing system-specific info.

Note: Before retrieving any environment variable in runtime environment, ensure it exists and can be reached from within it.

Example

public class EnvironmentVariableExample {
   public static void main(String[] args) {
      // Retrieving the value of the "USERNAME" environment variable
      String username = System.getenv("USERNAME");
        
      // Displaying the retrieved value
      System.out.println("Username: " + username);
   }
}

Output

Username: null

Conclusion

Accessing system properties and environment variables in Java provides a powerful method for gathering valuable configuration information and tailoring application behavior accordingly. The System.getProperty() method offers access to system properties related to installation, operating system settings and user settings while System.getEnv() gives access to environment variables which influence application behaviour with their dynamic values provided by either operating systems or users.

Leveraging these methods will enhance the flexibility, compatibility and customization of your Java apps to more closely fit system-related information and environments. Understanding system properties and environment variables enables developers to build robust applications capable of exploiting system information for improved adaptation.

Updated on: 25-Jul-2023

517 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements