difference between environment variables and System properties

tin_tin :

iam using the below link to understand environment variables and system properties.

https://docs.oracle.com/javase/tutorial/essential/environment/env.html

The link says environment variables are set by OS and passed to applications. When i fetch environment variables using System.getenv() it shows me lot of properties which i never set. So it must be OS (im using macOS) which had set these properties. Some of the properties in System.getenv() are MAVEN_CMD_LINE_ARGS, JAVA_MAIN_CLASS_1420, JAVA_MAIN_CLASS_1430.

My question is why would OS would like to set the java specific properties in environment variables? Ideally these should be set by JVM (in System.properties()).

P.S.: From whatever i have read on net i understand that environment variables are set by OS and System.properties() are set by JVM

Also if someone can point me to a good link on environment variable and System.properties it will be very helpful. Iam very confused between the two.

Andreas :

Environment variables is an OS concept, and are passed by the program that starts your Java program.

That is usually the OS, e.g. double-click in an explorer window or running command in a command prompt, so you get the OS-managed list of environment variables.

If another program starts your Java program1, e.g. an IDE (Eclipse, IntelliJ, NetBeans, ...) or a build tool (Maven, Groovy, ...), it can modify the list of environment variables, usually by adding more. E.g. the environment variable named MAVEN_CMD_LINE_ARGS would tend to indicate that you might be running your program with Maven.

In a running Java program, the list of environment variables cannot be modified.


System properties is a Java concept. The JVM will automatically assign a lot of system properties on startup.

You can add/override the values on startup by using the -D command-line argument.

In a running Java program, the list of system properties can be modified by the program itself, though that is generally a bad idea.


1) For reference, if a Java program wants to start another Java program, it will generally use a ProcessBuilder to set that up. The environment variables of the new Java process will by default be the same as the current Java program, but can be modified for the new Java program by calling the environment() method of the builder.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=366948&siteId=1