Running echo command from Java

11,211

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.

Share:
11,211
Maroun
Author by

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, 2022

Comments

  • Maroun
    Maroun almost 2 years

    I want to print the output of echo %path% from Java instead of cmd.

    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 the cmd 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
    Maroun over 11 years
    Indeed, I'm running it from Eclipse but as I stated in the question, I want to run it from Eclipse and not from the command line.
  • Evgeniy Dorofeev
    Evgeniy Dorofeev over 11 years
    Right, but this is Eclipse not Java who adds JRE to the PATH
  • Maroun
    Maroun over 11 years
    Thanks. Is there a way to prevent this?
  • Evgeniy Dorofeev
    Evgeniy Dorofeev over 11 years
    You can try to set PATH env in your test Run Configuration