如何枚举Java中的所有环境变量 [英] How to enumerate all environment variable in Java

查看:100
本文介绍了如何枚举Java中的所有环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我试图调用Runtime.exec(String[], String[], File),,辅助参数需要一个环境变量数组,如果我指定了此参数,我不确定子进程是否将从当前进程继承环境变量.

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.

例如,如果我将new String[]{"NEWDIR=/home"}作为辅助参数传递,并且当前的Java进程具有环境OLDDIR=/var,则子进程中System.getenv("OLDDIR")的返回值是什么?

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?

已更新: 抱歉,我必须使用Java 1.4,并且System.getenv()似乎是在1.5中引入的?

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

推荐答案

但是您可以轻松地切换到 ,这是启动新流程的更方便的API.

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

使用ProcessBuilder,您只需调用 environment() 并获得一个Map,其中包含现有环境变量,并且您可以操作自己想要的方式:即,如果添加东西,然后将其添加到新的流程环境变量中.如果您从其中删除某些内容,则新过程中将不存在.

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.

这篇关于如何枚举Java中的所有环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆