Understanding Java System Properties vs System Environment Variables and How to Modify Them
This article explains the distinction between Java system properties and operating‑system environment variables, shows how to read and set them in Java, and demonstrates advanced techniques using reflection to modify environment variables at runtime for the current process.
In Java development we usually configure parameters through configuration files or System Properties rather than using operating‑system System Variables , because properties are more flexible while variables are OS‑level environment entries.
System properties are JVM configuration parameters accessed via System.getProperty() . They are set when the JVM starts, either as internal JVM properties or via command‑line arguments such as -Dproperty=value .
System variables are OS‑level environment variables that can be used anywhere in the operating system. In Java they are accessed with System.getenv() . Common examples include PATH , HOME , and USERPROFILE .
Example code to obtain these values:
// 获取系统属性
String javaVersion = System.getProperty("java.version");
System.out.println("Java Version: " + javaVersion);
// 获取系统变量
String userHome = System.getenv("USERPROFILE"); // Windows
// 或者
String userHome = System.getenv("HOME"); // Unix/Linux
System.out.println("User Home Directory: " + userHome);Setting a system property in Java:
System.setProperty("name","认知科技技术团队");
System.out.println(System.getProperty("name"));System variables cannot be modified directly; they can be changed for the current process via reflection by accessing the underlying unmodifiable map:
public static void setEnv(String name, String value) throws Exception {
getTheUnmodifiableEnvironment().put(name, value);
}
public static void setEnvV2(String name, String value) throws Exception {
getTheUnmodifiableEnvironmentV2().put(name, value);
}
private static Map
getTheUnmodifiableEnvironment() throws Exception {
Class
pe = Class.forName("java.lang.ProcessEnvironment");
Method getenv = pe.getDeclaredMethod("getenv");
getenv.setAccessible(true);
Object unmodifiableEnvironment = getenv.invoke(null);
Class
map = Class.forName("java.util.Collections$UnmodifiableMap");
Field m = map.getDeclaredField("m");
m.setAccessible(true);
return (Map
) m.get(unmodifiableEnvironment);
}
private static Map
getTheUnmodifiableEnvironmentV2() throws Exception {
Class
pe = Class.forName("java.lang.ProcessEnvironment");
Field field = pe.getDeclaredField("theUnmodifiableEnvironment");
field.setAccessible(true);
Object unmodifiableEnvironment = field.get(null);
Class
map = Class.forName("java.util.Collections$UnmodifiableMap");
Field m = map.getDeclaredField("m");
m.setAccessible(true);
return (Map
) m.get(unmodifiableEnvironment);
}Test code demonstrating the utilities:
public static void main(String[] args) throws Exception {
EnvUtils.setEnv("author","认知科技技术团队");
System.out.println(System.getenv("author"));
System.out.println(System.getenv().get("author"));
EnvUtils.setEnvV2("微信公众号","认知科技技术团队");
System.out.println(System.getenv("微信公众号"));
System.out.println(System.getenv().get("微信公众号"));
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+System.getenv("微信公众号"));
System.out.println(Thread.currentThread().getName()+System.getenv().get("微信公众号"));
}
});
thread.start();
thread.join();
}Note: Newer Java versions (e.g., Java 21) require adding module‑opening command‑line arguments in IDEs such as IntelliJ IDEA, for example:
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED --add-opens java.base/java.math=ALL-UNNAMED --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/java.security=ALL-UNNAMED --add-opens java.base/java.text=ALL-UNNAMED --add-opens java.base/java.time=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/jdk.internal.access=ALL-UNNAMED --add-opens java.base/jdk.internal.misc=ALL-UNNAMEDFor comparison, a Python snippet shows that modifying environment variables is simpler:
import os
if __name__ == '__main__':
os.environ['author'] = '认知科技技术团队'
print(os.getenv('author'))
print(os.environ.get('author'))
print(os.environ['author'])Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.