System.getenv()和 System.getProperty()的区别

可能的复制品:
System 属性和环境变量 的区别是什么

System.getenv ()和 System.getProperty ()之间的区别是什么?

当我们使用 ProcessBuilder 运行任何命令时,我们可以设置环境变量,即:

String[] vCmd = { System.getenv("ANT_HOME") + "/bin/ant", "-f",
ANT_BUILD_FILE, TARGET };
ProcessBuilder pb = new ProcessBuilder(vCmd);
Map<String, String> env = pb.environment();
env.put("CLASSPATH",
antHome+"/lib/ant.jar:"
+ antHome+"/lib/ant-launcher.jar:"
+ antHome+"/lib/ant-nodeps.jar:"
);
try{


Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
pb.wait();
}
catch(Exception e)


System.out.println(e.getMessage());




}

如果我在此方法之前使用 System.setProperties ()设置一些属性,它是否可用于由 ProcessBuilder 启动的此进程?

89309 次浏览

System.getenv() is for Operating System environment variables, whereas System.getProperty() is for JVM arguments which are passed as -DpropName=value to Java application launcher (java).

System.getenv gets an environment variable. System.getProperty gets a Java property. Environment variables are specified at the OS level. Java properties are specified by passing the -D option to the JVM (and can be set programmatically).