Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java system properties and environment variables

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

like image 866
Praveen Sripati Avatar asked Aug 14 '11 04:08

Praveen Sripati


People also ask

Is system property same as environment variable?

System properties are specified with command-line options in Java using the -Dname=value while environment variables are set in the operating system, using the EXPORT command in Unix and SET command in windows. To get a specific system property, we can use the System.

How do I set system properties in environment variables?

To get a specific system property you can use System. getProperty(String key) or System. getProperty(String key, String def) . Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.

What are system properties in Java?

System properties include information about the current user, the current version of the Java runtime, and the character used to separate components of a file path name. Character that separates components of a file path.


2 Answers

  • System properties are set on the Java command line using the -Dpropertyname=value syntax. They can also be added at runtime using System.setProperty(String key, String value) or via the various System.getProperties().load() methods.
    To get a specific system property you can use System.getProperty(String key) or System.getProperty(String key, String def).

  • Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.
    To get a specific environment variable you can use System.getenv(String name).

like image 99
Bohemian Avatar answered Sep 29 '22 15:09

Bohemian


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

like image 39
Jake Dempsey Avatar answered Sep 29 '22 17:09

Jake Dempsey