What's the difference between system properties System.getProperties() and environment variables System.getenv() in a JVM?
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()
.
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)
.
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 JAVA_TOOL_OPTIONS
. –
Hyperparathyroidism System.getenv(String name)
does not dynamically read the value from the system at call time. –
Praseodymium 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()
.
© 2022 - 2024 — McMap. All rights reserved.