Java system properties and environment variables
Solution 1
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()
.
Solution 2
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)
.
Related videos on Youtube
Praveen Sripati
Very passionate about the intersection of Big Data and Cloud technologies. I am a Cloudera Certified Developer for Apache Hadoop, Hortonworks Certified Apache Hadoop Java Developer, AWS Certified Solutions Architect - Associate and AWS Certified Developer - Associate. If interested Consulting/Projects/Trainings around Cloud and Big Data, please contact at [email protected]. Currently I am conducting a training on AWS Development, more details here. I started blogging for fun and started liking it. So, I regularly blog at thecloudavenue.com around Big Data, K8S and Cloud related technologies. I also Tweet here.
Updated on October 30, 2020Comments
-
Praveen Sripati over 3 years
What's the difference between system properties System.getProperties() and environment variables System.getenv() in a JVM?
-
paulsm4 over 12 yearsAbsolutely 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.
-
Praveen Sripati over 12 yearsFinally, it's how the variables are added and the scope of the variables.
-
Marek Sebera almost 11 years@Bohemian If I set property via
java -Dpropname=value
how can i then retrieve those properties? -
Bohemian almost 11 years
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. -
flacs over 9 yearsNote that you can also set system properties with the environment variable
JAVA_TOOL_OPTIONS
. -
Kanagavelu Sugumar almost 7 yearsDo we need JVM restart to read the updated env variable ? I know for restart required to read properties updated by external source.
-
Bohemian almost 7 years@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. -
malana over 6 years@paulsm4 what do you mean by "Java chose to expose OS variables as properties"?
-
Christian almost 4 yearsKeep in mind that other processes can find the cmd used to launch a process, hence java system properties as well.
-
Andy Cribbens about 3 yearsThere 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.
-
Pedro Lamarão about 3 yearsThis answer seems incorrect. Environment variables are scoped per process. Each process sees its own environment.
-
Koray Tugay over 2 years@PedroLamarão Are you sure? As far as I can tell environment variables are set on the host and are visible to all processes..
-
Koray Tugay over 2 years@JakeDempsey Are you sure env variables are set in the OS (however they 'can' be set through Java) is a correct statement?
-
Pedro Lamarão over 2 yearsThe 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.