Difference between System.getenv() & System.getProperty() [duplicate]
Asked Answered
M

2

109

Possible Duplicate:
What’s the difference between a System property and environment variable

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

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());


        }

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

Marielamariele answered 28/10, 2012 at 19:5 Comment(0)
M
209

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

Mcinerney answered 28/10, 2012 at 19:8 Comment(0)
B
40

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

Basel answered 28/10, 2012 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.