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启动的此过程吗?
getenv获取 环境变量 。getProperty获取Java属性。环境变量是在OS级别指定的。通过将-D选项传递给JVM 来指定Java属性(并且可以通过编程设置)。
getenv
getProperty
-D