Search code examples
javaprocessprocessbuilderio-redirection

Redirect output of echo to file through Java


Possible Duplicate:
ProcessBuilder redirecting output

The following code:

ProcessBuilder pb = new ProcessBuilder(new String[] {"echo", "some text", ">", "test"});

keeps returning "some text > test".

What am I doing wrong?

EDIT:

this worked

ProcessBuilder pb = new ProcessBuilder(new String[] {"bash", "-c", "echo sometext > test"});

Solution

  • Try following

        ProcessBuilder pb = 
    new ProcessBuilder("cmd.exe", "/c" ,"echo", "some text", ">", "test");
    

    This is for windows

    Actually 'Echo' is not a command its an internal command of the shell (cmd.exe) in windows and "bash" in linux or unix. So , for Unix/Linux

        ProcessBuilder pb = 
    new ProcessBuilder("bash", "-c","echo \"some text\" >test");