Explain Codes LogoExplain Codes Logo

How can I get System variable value in Java?

java
environment-variables
system-properties
java-8
Alex KataevbyAlex Kataev·Oct 23, 2024
TLDR

To access a system property, use System.getProperty("property_name"). To fetch an environment variable, utilize System.getenv("variable_name"). Here's an example:

String javaVersion = System.getProperty("java.version"); System.out.println("Java Version: " + javaVersion); // This outputs the Java version

You can also list all properties or all environment variables using System.getProperties() or System.getenv(). But ensure no typos are in your variable names, as the accuracy of these retrievals heavily rely on this. Remember, System.getProperty() and System.getenv() access Java system properties, and environment variables respectively.

Schrodinger's variables: System properties and environment variables

Java offers a duo of key utilities to access configuration information - system properties and environment variables. Passed to the JVM via -D<name>=<value> command-line arguments or programmatically using System.setProperty(), system properties are akin to whispered secrets to the JVM.

Accessed using System.getenv(), environment variables are set at the operating system level and should be made visible prior running the Java app (like hiding Easter eggs before the hunt begins). They can be double-checked using commands such as echo $VARIABLE (Unix-like systems) or echo %VARIABLE% (Windows).

When dealing with values that could end up as null, the Java 8 Optional class offers a smooth ride:

String dbEnvVar = Optional.ofNullable(System.getenv("DBE")) .orElseThrow(() -> new IllegalStateException("DBE not defined. Did you forget?"));

This throws a custom exception if the variable is not found. Often, environment variable changes need a system re-initialization or restart. Remember, Java isn't psychic!

Going beyond the basics: Robust coding practices

When Null is not your friend

In an uncertain world, defensive programming is key. When retrieving variables that may not exist, null checks are your best friend:

String value = System.getenv("UNDEFINED_MAYBE"); if (value != null) { // Use the value and celebrate 🎉 } else { // Null showed up uninvited. Party's over folks! 🙁 }

This is where wrapping the call in Optional adds shades of resiliency. It allows defining a default behavior or custom exception when the variable is missing. It’s like having a backup plan for a backup plan.

When System.getProperty() saves the day

While System.getenv() is great for fetching environment variables, System.getProperty() can bring home the system properties:

String userHome = System.getProperty("user.home");

These can be set with JVM startup and also fetched with System.getProperties(). It’s like asking Java for your bag of variables back!

The Deprecated Phoenix

The System.getenv(String) function was like a Phoenix, deprecated in early Java versions but reignited in Java SE 5. Always refer to the Java documentation for the latest information. It's the Hogwarts library of the muggle world!

Red alerts on the radar: Troubleshooting and good practices

Check your glasses: Variable visibility

Always double-check the visibility of your variable in the system. It’s like checking whether you left your keys on the table or in the fridge (we've all been there).

Not all systems are created equal

System-specific nuances—like the difference in setting and accessing environment variables on different operating systems—could trip you up faster than a loose shoelace.

Not all secrets should be shared

Consider security implications. Since environment variables might store sensitive information (like your favorite pizza topping), make sure they aren’t exposed to those who should not know. Remember, with great power comes great responsibility!