What's the difference between a System property and environment variable
Asked Answered
S

2

47

I am not clear about this. When I run a java App or run an Applet in applet viewer, (in the IDE environment), System.getProperty("java.class.path") gives me the same as System.getenv("CLASSPATH") which is the CLASSPATH defined in my env variable.

But when I deploy my applet to webserver and access it from the same computer as a client, I get different results for the two. (System.getProperty("java.class.path") only points to JRE home and System.getenv("CLASSPATH") returns null).

And here is some other things that make me wonder:

For the applet part, the env var JAVA_HOME, I get the same result when deploying the applet in a browser as well as Applet Viewer.

And if I define myself a env variable at system level, and use getenv("envName") the result is null. Is there anyway I can define one and get it in my Java program?

Secundines answered 19/5, 2010 at 7:45 Comment(2)
I keep making mistake System.getProperty("java.io.tmpdir") vs System.getenv("TEMP")Childress
For people coming to this question in the future, this question was marked as a duplicate and has some good answers as well.Impart
S
55

Environment variables are specific to the operating system. Properties are JVM only.

Sheepdog answered 19/5, 2010 at 7:47 Comment(2)
I still dont' understand if env var is of operating system, why my code in the applet getting the env var of classpath return different result in the two case. The difference in the two case is only the JVM right?Secundines
Have you seen javadoc for System.getenv? I think it does make a difference whether you run it from browser, IDE or console. Each of these can have different environment.Sheepdog
I
33
System.getProperty("Propertname") **Platform Independent** 

The above method will return JVM arguments and properties.

System.getenv("EnvName")       **Platform Dependent**

The above method returns your operating system environment variables.

In Linux you can set a environment variable from the shell using the following command.

export SYSTEM_TYPE=PROD

In Java you can read the variable by

System.getenv("SYSTEM_TYPE")

The above code will return PROD

http://javarevisited.blogspot.in/2012/08/how-to-get-environment-variables-in.html

Impetus answered 24/5, 2013 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.