Java
System Properties
Environment Variables
Programming
Software Development

Java system properties and environment variables

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In the world of Java programming, understanding the distinction and usage of system properties and environment variables is essential for configuring applications effectively. Both system properties and environment variables provide configuration or parameter information to the Java Virtual Machine (JVM) and the application running on it. However, they originate from slightly different sources and serve different purposes in a Java environment.

System Properties

System properties are key-value pairs managed by the JVM. They can represent configuration values specific to the Java environment, such as user settings, Java version, or configuration details about the Java runtime environment. In Java, these properties can be set in several ways:

  1. Directly via the command line at application launch using the -D option. For example, you can specify java -DmyKey=myValue MyApplication to set the myKey property to myValue.
  2. Programmatically, within your Java code using System.setProperty(String key, String value).
  3. From a properties file, loaded at runtime.

To retrieve these properties, you would use System.getProperty(String key) or System.getProperty(String key, String def) which provides a default value if the property is not found.

Example:

java
1public class SystemPropertiesExample {
2    public static void main(String[] args) {
3        // Set a system property programmatically
4        System.setProperty("myNewProperty", "12345");
5
6        // Retrieve a property
7        String propertyValue = System.getProperty("myNewProperty");
8        System.out.println("The value of myNewProperty is " + propertyValue);
9
10        // Default system property
11        String nonExistingProperty = System.getProperty("nonExisting", "defaultValue");
12        System.out.println("The value of nonExisting is " + nonExistingProperty);
13    }
14}

Environment Variables

Environment variables are variables external to the JVM and are set at the operating system level. These variables can influence the behavior of the JVM and the Java application. They are particularly useful for including external system-level information into the application, such as configurations that are sensitive (e.g., passwords, API keys) or dependent on the specific execution environment (test, production, development).

Java can access these variables via the System.getenv(String name) method, which reads the value of a specified environment variable, or System.getenv() which returns a map of all environment variables.

Example:

java
1public class EnvironmentVariablesExample {
2    public static void main(String[] args) {
3        // Getting an environment variable
4        String pathVariable = System.getenv("PATH");
5        System.out.println("PATH = " + pathVariable);
6
7        // Map of all environment variables
8        Map<String, String> envs = System.getenv();
9        for (String envName : envs.keySet()) {
10            System.out.println(envName + " = " + envs.get(envName));
11        }
12    }
13}

Table Summary of Key Differences

FeatureSystem PropertiesEnvironment Variables
Set ByJVM or Java application.Operating system or external processes.
ScopeOnly accessible within Java applications.Accessible by any process in the system.
Typical Use CaseConfiguration of the Java environment and application specifics.Configuration including sensitive and external system information.
Access in JavaSystem.getProperty(key)System.getenv(name)
Command Line SpecificationVia -D option (e.g., -DexampleKey=exampleValue).Set in the OS before starting the JVM or the application.

Use Cases and Best Practices

While both system properties and environment variables can be used to configure Java applications, best practices suggest using environment variables for sensitive data like database passwords or third-party API keys. This minimizes risks associated with code changes and version control visibility. System properties, on the other hand, are ideal for setting configuration that might change more regularly, or that is specific to the JVM or a particular application instance.

Understanding and leveraging both system properties and environment variables effectively allows Java developers to build flexible, secure, and robust applications adaptable to various environments without altering the core application logic. This separation of concerns facilitates easier application maintenance and deployment across different systems.


Course illustration
Course illustration

All Rights Reserved.