My os is windows7. I want to read the environment variables in my Java application. I have searched google and many people's answer is to use the method System.getProperty(String name)
or System.getenv(String name)
. But it doesn't seem to work. Through the method, I can read some variable's value that defined in the JVM.
我的操作系统是windows7。我想在Java应用程序中读取环境变量。我搜索过谷歌,很多人的回答是使用方法System.getProperty(String name)或System.getenv(String name)。但它似乎没有用。通过该方法,我可以读取JVM中定义的一些变量值。
If I set an environment variable named "Config", with value "some config infomations", how can I get the value in Java?
如果我设置一个名为“Config”的环境变量,其值为“some config infomations”,如何在Java中获取该值?
Thanks for any help!
谢谢你的帮助!
2 个解决方案
#1
55
You should use System.getenv(), for example:
您应该使用System.getenv(),例如:
import java.util.Map;
public class EnvMap {
public static void main (String[] args) {
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n",
envName,
env.get(envName));
}
}
}
When running from an IDE you can define additional environment variable which will be passed to your Java application. For example in IntelliJ IDEA you can add environment variables in the "Environment variables" field of the run configuration.
从IDE运行时,您可以定义将传递给Java应用程序的其他环境变量。例如,在IntelliJ IDEA中,您可以在运行配置的“环境变量”字段中添加环境变量。
Notice (as mentioned in the comment by @vikingsteve) that the JVM, like any other Windows executable, system-level changes to the environment variables are only propagated to the process when it is restarted.
For more information take a look at the "Environment Variables" section of the Java tutorial.System.getProperty(String name)
is intended for getting Java system properties which are not environment variables.
注意(如@vikingsteve的评论中所述),JVM与任何其他Windows可执行文件一样,对环境变量的系统级更改仅在重新启动时传播到进程。有关更多信息,请查看Java教程的“环境变量”部分。 System.getProperty(String name)用于获取非环境变量的Java系统属性。
#2
41
In case anyone is coming here and wondering how to get a specific environment variable without looping through all of your system variables you can use getenv(String name)
. It returns "the string value of the variable, or null if the variable is not defined in the system environment".
如果有人来到这里并想知道如何在不循环遍历所有系统变量的情况下获取特定环境变量,则可以使用getenv(String name)。它返回“变量的字符串值,如果未在系统环境中定义变量,则返回null”。
String myEnv = System.getenv("env_name");
#1
55
You should use System.getenv(), for example:
您应该使用System.getenv(),例如:
import java.util.Map;
public class EnvMap {
public static void main (String[] args) {
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n",
envName,
env.get(envName));
}
}
}
When running from an IDE you can define additional environment variable which will be passed to your Java application. For example in IntelliJ IDEA you can add environment variables in the "Environment variables" field of the run configuration.
从IDE运行时,您可以定义将传递给Java应用程序的其他环境变量。例如,在IntelliJ IDEA中,您可以在运行配置的“环境变量”字段中添加环境变量。
Notice (as mentioned in the comment by @vikingsteve) that the JVM, like any other Windows executable, system-level changes to the environment variables are only propagated to the process when it is restarted.
For more information take a look at the "Environment Variables" section of the Java tutorial.System.getProperty(String name)
is intended for getting Java system properties which are not environment variables.
注意(如@vikingsteve的评论中所述),JVM与任何其他Windows可执行文件一样,对环境变量的系统级更改仅在重新启动时传播到进程。有关更多信息,请查看Java教程的“环境变量”部分。 System.getProperty(String name)用于获取非环境变量的Java系统属性。
#2
41
In case anyone is coming here and wondering how to get a specific environment variable without looping through all of your system variables you can use getenv(String name)
. It returns "the string value of the variable, or null if the variable is not defined in the system environment".
如果有人来到这里并想知道如何在不循环遍历所有系统变量的情况下获取特定环境变量,则可以使用getenv(String name)。它返回“变量的字符串值,如果未在系统环境中定义变量,则返回null”。
String myEnv = System.getenv("env_name");