Java system properties and environment variables
Asked Answered
O

2

364

What's the difference between system properties System.getProperties() and environment variables System.getenv() in a JVM?

Occasionalism answered 14/8, 2011 at 4:18 Comment(0)
C
188

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().

Clayberg answered 14/8, 2011 at 4:30 Comment(8)
Finally, it's how the variables are added and the scope of the variables.Occasionalism
Keep in mind that other processes can find the cmd used to launch a process, hence java system properties as well.Instancy
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.Clotilde
This answer seems incorrect. Environment variables are scoped per process. Each process sees its own environment.Hixson
@PedroLamarão Are you sure? As far as I can tell environment variables are set on the host and are visible to all processes..Mur
@JakeDempsey Are you sure env variables are set in the OS (however they 'can' be set through Java) is a correct statement?Mur
The 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.Hixson
So, to summarize the comment thread, there is no conceptual difference between Java system properties and Unix/Windows environment variables. System properties are just an analogous, platform-independent API for the same purpose.Atmospherics
P
494
Praseodymium answered 14/8, 2011 at 4:20 Comment(7)
Absolutely 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.Gammy
@Praseodymium If I set property via java -Dpropname=value how can i then retrieve those properties?Piotr
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.Praseodymium
Note that you can also set system properties with the environment variable JAVA_TOOL_OPTIONS.Hyperparathyroidism
Do we need JVM restart to read the updated env variable ? I know for restart required to read properties updated by external source.Ertha
@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.Praseodymium
@Gammy what do you mean by "Java chose to expose OS variables as properties"?Landa
C
188

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().

Clayberg answered 14/8, 2011 at 4:30 Comment(8)
Finally, it's how the variables are added and the scope of the variables.Occasionalism
Keep in mind that other processes can find the cmd used to launch a process, hence java system properties as well.Instancy
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.Clotilde
This answer seems incorrect. Environment variables are scoped per process. Each process sees its own environment.Hixson
@PedroLamarão Are you sure? As far as I can tell environment variables are set on the host and are visible to all processes..Mur
@JakeDempsey Are you sure env variables are set in the OS (however they 'can' be set through Java) is a correct statement?Mur
The 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.Hixson
So, to summarize the comment thread, there is no conceptual difference between Java system properties and Unix/Windows environment variables. System properties are just an analogous, platform-independent API for the same purpose.Atmospherics

© 2022 - 2024 — McMap. All rights reserved.