Run Unix commands from Java

Greeings all

Im trying to excute a command from Java and direct the output to the main output screen or to another file .... can you please help with this ? can I use filewriter for this ?

Here is my code....

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class RunCommand {

    private static boolean str;

    public static void main(String args[]) throws IOException {

        File dir = new File("c:\\Redalert");
        Process proc = Runtime.getRuntime().exec("cmd /c  start dir", null, dir);
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));

          //  FileWriter fw = new FileWriter("c:\\output.txt");

        while (str = stdInput.readLine() != null) {
            System.out.println(str);

        }
    }
}

One way:

String[] cmd_elements = {"ls", "-l"};

Process prcs = Runtime.getRuntime().exec(cmd_elements);

InputStream cmd_output = prcs.getInputStream();

Process the inputstream to another file

Thank you for the replay, but can you please explain more ?

Im already doing this is in "new InputStreamReader(proc.getInputStream())"

Thanks again

The difference would appear to be the way he's calling exec() there -- giving it an array. It's not a shell -- it's not smart enough to split the string for you.