Java 系统属性和环境变量有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2863674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between a System property and environment variable
提问by Khue Vu
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.
我不清楚这一点。当我在小程序查看器中运行 Java 应用程序或运行小程序时(在 IDE 环境中), System.getProperty("java.class.path")
给我的结果与System.getenv("CLASSPATH")
在我的 env 变量中定义的 CLASSPATH相同。
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).
但是当我将我的小程序部署到网络服务器并从作为客户端的同一台计算机访问它时,我得到了不同的结果。(System.getProperty("java.class.path")
仅指向 JRE home 并System.getenv("CLASSPATH")
返回 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.
对于小程序部分,环境变量 JAVA_HOME,在浏览器和小程序查看器中部署小程序时,我得到了相同的结果。
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?
如果我在系统级别定义自己一个 env 变量,并使用getenv("envName")
结果是null
. 无论如何我可以定义一个并在我的Java程序中获取它吗?
采纳答案by Konrad Garus
Environment variables are specific to the operating system. Properties are JVM only.
环境变量特定于操作系统。属性仅为 JVM。
回答by Yuvaraj Loganathan
System.getProperty("Propertname") **Platform Independent**
The above method will return JVM arguments and properties.
上述方法将返回 JVM 参数和属性。
System.getenv("EnvName") **Platform Dependent**
The above method returns your operating system environment
variables.
上述方法返回您的操作系统environment
变量。
In Linux you can set a environment variable from the shell using the following command.
在 Linux 中,您可以使用以下命令从 shell 设置环境变量。
export SYSTEM_TYPE=PROD
In Java you can read the variable by
在 Java 中,您可以通过以下方式读取变量
System.getenv("SYSTEM_TYPE")
The above code will return PROD
上面的代码会返回 PROD
http://javarevisited.blogspot.in/2012/08/how-to-get-environment-variables-in.html
http://javarevisited.blogspot.in/2012/08/how-to-get-environment-variables-in.html