Redirecting input and output from operating system's processes - Java Native OS

Java examples for Native OS:Process

Description

Redirecting input and output from operating system's processes

Demo Code

import java.io.File;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        try {/*from   w  w w  . j  a v a  2 s.c o m*/
        File commands = new File("C:/Projects/ProcessCommands.txt");
        File output = new File("C:/Projects/ProcessLog.txt");
        File errors = new File("C:/Projects/ErrorLog.txt");

        ProcessBuilder pb = new ProcessBuilder("cmd");
        System.out.println(pb.redirectInput().toString());
        System.out.println(pb.redirectOutput().toString());
        System.out.println(pb.redirectError().toString());

        pb.redirectInput(commands);
        pb.redirectError(errors);
        pb.redirectOutput(output);
        System.out.println(pb.redirectInput().toString());
        System.out.println(pb.redirectOutput().toString());
        System.out.println(pb.redirectError().toString());

        pb.start();
        } 
        catch(IOException ex) {
            ex.printStackTrace();
        }
    }
}

Related Tutorials