Environment variables and properties in Java are essential components for configuring applications without hardcoding values. They enable Java applications to access system-specific values and define runtime configurations. Both environment variables and properties serve similar purposes but differ in their scope and handling.
1. Environment Variables
Environment variables are system-wide settings that provide information about the environment in which a program executes. These variables are typically set by the operating system and can be accessed by any application running on that system.
Accessing Environment Variables in Java
Java provides a way to access environment variables using the System.getenv()
method. This method returns a Map<String, String>
containing all the environment variables.
import java.util.Map; public class EnvironmentVariablesExample { public static void main(String[] args) { Map<String, String> env = System.getenv(); System.out.println("Environment Variables:"); for (String key : env.keySet()) { System.out.println(key + " = " + env.get(key)); } // Accessing a specific environment variable String path = System.getenv("PATH"); System.out.println("PATH: " + path); } }
In the above program, we access all the environment variables and display them. Additionally, we access the PATH
variable specifically.
2. Properties
Properties in Java are key-value pairs used to configure application settings at runtime. They are not system-wide like environment variables but are specific to the application. Java provides the java.util.Properties
class to handle properties.
Creating and Accessing Properties
Properties can be loaded from a file or defined programmatically. The Properties
class provides methods to read, write, and manipulate property data.
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class PropertiesExample { public static void main(String[] args) { Properties properties = new Properties(); try (FileOutputStream output = new FileOutputStream("config.properties")) { properties.setProperty("username", "LotusJavaPrince"); properties.setProperty("database", "mahesh_db"); properties.store(output, "Application Configuration"); } catch (IOException e) { e.printStackTrace(); } try (FileInputStream input = new FileInputStream("config.properties")) { properties.load(input); System.out.println("Username: " + properties.getProperty("username")); System.out.println("Database: " + properties.getProperty("database")); } catch (IOException e) { e.printStackTrace(); } } }
In this example, a config.properties
file is created, properties are added, and then they are read and displayed.
Environment variables and properties in Java are powerful mechanisms for configuring applications dynamically. While environment variables provide system-level information, properties offer application-specific configuration in a flexible key-value format. Understanding both is crucial for building maintainable and configurable Java applications.