如何枚举Java中的所有环境变量

时间:2022-06-08 12:06:42

System.getenv(name) needs the name of environment variable.

System.getenv(名称)需要环境变量的名称。

I am trying to call Runtime.exec(String[], String[], File), the secondary parameter need an array of environment variable, I am not sure whether subprocess will inherit environment variables from current process if I specified this parameter.

我正在调用运行时。exec(String[], String[], File),次要参数需要一个环境变量数组,我不确定如果我指定这个参数,子进程是否会继承当前进程的环境变量。

For example, if I pass new String[]{"NEWDIR=/home"} as secondary parameter and current java process has environment OLDDIR=/var, what is the return value of System.getenv("OLDDIR") in the subprocess?

例如,如果我将新的字符串[]{"NEWDIR=/home"}作为辅助参数,并且当前java进程具有environment OLDDIR=/var,子进程中System.getenv("OLDDIR")的返回值是多少?

updated: Sorry, I have to use Java 1.4 and it seems that System.getenv() was introduced in 1.5?

更新:抱歉,我必须使用Java 1.4,看来System.getenv()是在1.5中引入的?

3 个解决方案

#1


13  

System.getenv() will return a Map<String,String> with all of the environment variables.

getenv()将返回映射 和所有环境变量。 ,string>

But you could just as easily switch to ProcessBuilder which is a more convenient API to start new processes.

但是您也可以很容易地切换到ProcessBuilder,它是启动新进程的更方便的API。

With ProcessBuilder you can simply call environment() and get a Map that contains existing environment variables and which you can manipulate how you want: i.e., if you add something to it, then that will be added to the new processes environment variables. If you remove something from it, it will not be present in the new process.

使用ProcessBuilder,您可以简单地调用environment()并获得一个包含现有环境变量的映射,您可以操作您想要的方式:例如。,如果您向它添加一些东西,那么它将被添加到新的流程环境变量中。如果您从它中删除一些内容,它将不会出现在新的进程中。

#2


21  

Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
    System.out.format("%s=%s%n", envName, env.get(envName));
}

#3


1  

If you run an external shell, you can use it to set environment variables. e.g.

如果运行外部shell,可以使用它设置环境变量。如。

bash -c ENV1=hi ENV2=bye echo $ENV1 $ENV2

This only works if you have a UNIX shell (or cygwin)

只有当您有一个UNIX shell(或cygwin)时,这才有效。

You should migrate away from Java 1.4 and Java 5.0. Even Java 6 you might consider upgrading to Java 7.

您应该远离Java 1.4和Java 5.0。即使是Java 6,您也可以考虑升级到Java 7。

#1


13  

System.getenv() will return a Map<String,String> with all of the environment variables.

getenv()将返回映射 和所有环境变量。 ,string>

But you could just as easily switch to ProcessBuilder which is a more convenient API to start new processes.

但是您也可以很容易地切换到ProcessBuilder,它是启动新进程的更方便的API。

With ProcessBuilder you can simply call environment() and get a Map that contains existing environment variables and which you can manipulate how you want: i.e., if you add something to it, then that will be added to the new processes environment variables. If you remove something from it, it will not be present in the new process.

使用ProcessBuilder,您可以简单地调用environment()并获得一个包含现有环境变量的映射,您可以操作您想要的方式:例如。,如果您向它添加一些东西,那么它将被添加到新的流程环境变量中。如果您从它中删除一些内容,它将不会出现在新的进程中。

#2


21  

Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
    System.out.format("%s=%s%n", envName, env.get(envName));
}

#3


1  

If you run an external shell, you can use it to set environment variables. e.g.

如果运行外部shell,可以使用它设置环境变量。如。

bash -c ENV1=hi ENV2=bye echo $ENV1 $ENV2

This only works if you have a UNIX shell (or cygwin)

只有当您有一个UNIX shell(或cygwin)时,这才有效。

You should migrate away from Java 1.4 and Java 5.0. Even Java 6 you might consider upgrading to Java 7.

您应该远离Java 1.4和Java 5.0。即使是Java 6,您也可以考虑升级到Java 7。