What's the difference between system properties System.getProperties() and environment variables System.getenv() in a JVM?
2 Answers
System properties are set on the Java command line using the
-Dpropertyname=value
syntax. They can also be added at runtime usingSystem.setProperty(String key, String value)
or via the variousSystem.getProperties().load()
methods.
To get a specific system property you can useSystem.getProperty(String key)
orSystem.getProperty(String key, String def)
.Environment variables are set in the OS, e.g. in Linux
export HOME=/Users/myusername
or on WindowsSET WINDIR=C:\Windows
etc, and, unlike properties, may not be set at runtime.
To get a specific environment variable you can useSystem.getenv(String name)
.
-
60Absolutely correct, Bohemian. Environment variables are an "OS thing", and properties are a "Java thing". As it happens, Java chose to expose OS variables as properties (just as Java exposes current directory and "other stuff" as properties), but they are in fact different things.– paulsm4Commented Aug 14, 2011 at 4:29
-
1@Bohemian If I set property via
java -Dpropname=value
how can i then retrieve those properties? Commented Jul 25, 2013 at 10:08 -
9
System.grtProperties()
lists all properties, and those set from command line will be there, but there's no way to distinguish those from the other properties added by the system, if that's what you're asking.– Bohemian ♦Commented Jul 25, 2013 at 10:13 -
18Note that you can also set system properties with the environment variable
JAVA_TOOL_OPTIONS
.– user272237Commented Oct 31, 2014 at 15:19 -
7@KanagaveluSugumar Yes, you need to restart: Environment variable settings are read from the environment on start up. i.e.
System.getenv(String name)
does not dynamically read the value from the system at call time.– Bohemian ♦Commented May 25, 2017 at 16:01
I think the difference between the two boils down to access. Environment variables are accessible by any process and Java system properties are only accessible by the process they are added to.
Also as Bohemian stated, env variables are set in the OS (however they 'can' be set through Java) and system properties are passed as command line options or set via setProperty()
.
-
8Finally, it's how the variables are added and the scope of the variables. Commented Aug 14, 2011 at 12:33
-
Keep in mind that other processes can find the cmd used to launch a process, hence java system properties as well. Commented Jun 13, 2020 at 11:23
-
There is more to it. This tutorial explains in detail: youtu.be/vQYfOMrdgpg - Basically env vars can also have scope, e.g. set in one shell may not be visible in another. You typically cannot set them at runtime because they are on the host, however you can set them (at runtime) in JUnit 5 using extensions etc. Commented Mar 3, 2021 at 10:09
-
3This answer seems incorrect. Environment variables are scoped per process. Each process sees its own environment. Commented Apr 7, 2021 at 14:17
-
3The environment variable map is a per process object in Windows and every UNIX descendant. It is best to think about is a "process attribute" or some kind of process private thing. This map is created when the process is created. The initial values are set by whomever creates the process. Typically, this map shall be a copy of the creator's map. User applications are generally created by the user shell, therefore, user application's environment shall generally by a copy of the user shell's environment. There is not dynamic inheritance here, no "fall back to parent" mechanism. Commented Sep 14, 2021 at 20:57