System.getenv() & 之间的区别System.getProperty()

2022-01-16 00:00:00 environment-variables java

可能重复:
系统属性和环境变量有什么区别

System.getenv() & 有什么区别?System.getProperty()?

What is the difference between System.getenv() & System.getProperty()?

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

When we run any command using Processbuilder, we can set the environment variables ie:

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 启动的这个进程?

If I set some properties using System.setProperties() before this method , is it available to this process started by ProcessBuilder?

推荐答案

System.getenv 得到一个环境变量.System.getProperty 获取 Java 属性.环境变量在操作系统级别指定.Java 属性是通过将 -D 选项传递给 JVM 来指定的(并且可以通过编程方式设置).

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).

相关文章