Running echo command from Java
Solution 1
You are probably running your test from IDE (eg Eclipse). Try the same from command line. BTW there is another way to print environnment variables from Java
System.out.println(System.getenv("PATH"));
Solution 2
Looks like Java
appends to %path%
its own paths. Nothing else.
Maroun
A passionate pianist and software engineer. Why Stack Overflow is part of my daily routine My first and last name are identical
Updated on June 04, 2022Comments
-
Maroun almost 2 years
I want to print the output of
echo %path%
fromJava
instead ofcmd
.I have the following code:
private void getPath() throws IOException { String getPath = "cmd.exe /C echo %path%"; Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(getPath); BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); String commandOutput = ""; while (commandOutput != null) { commandOutput = reader.readLine(); System.out.println(commandOutput); } }
If I run
echo %path%
from thecmd
the output begins with:C:\Oracle\Ora11\bin;C:\Oracle\Ora10\bin;C:\Program Files\Common
But the output of the
Java
program begins with:C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386
and only after this line, the rest of the output is similar.
Why is this happening?
-
Maroun over 11 yearsIndeed, I'm running it from
Eclipse
but as I stated in the question, I want to run it fromEclipse
and not from the command line. -
Evgeniy Dorofeev over 11 yearsRight, but this is Eclipse not Java who adds JRE to the PATH
-
Maroun over 11 yearsThanks. Is there a way to prevent this?
-
Evgeniy Dorofeev over 11 yearsYou can try to set PATH env in your test Run Configuration