Difference between System.getenv() & System.getProperty()

JavaEnvironment Variables

Java Problem Overview


> 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?

Java Solutions


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

Solution 2 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1731553View Question on Stackoverflow
Solution 1 - JavaT.J. CrowderView Answer on Stackoverflow
Solution 2 - JavaBhesh GurungView Answer on Stackoverflow