Redirecting input and output from operating systems processes : JDK 7 « JDK 7 « Java






Redirecting input and output from operating systems processes


import java.io.File;

public class Test {

  public static void main(String[] args) throws Exception {
    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();
  }
}

 








Related examples in the same category

1.Using the platform MXBeans for a JVM for system and process load monitoring