你好: 在百度上看到你的回答我很满意,我想请教你一点问题.你在吗?

2024-11-13 13:31:28
推荐回答(1个)
回答1:

package needl;
import java.io.*;
import java.util.concurrent.*;
/**
 * @author Hardneedl
 */
final class ScriptRunDemo {
    public static void main(String[]args) throws IOException, InterruptedException {
        //creat demo shell script
        PrintWriter fout = new PrintWriter("scriptDemo.cmd");
        fout.print("DIR C:\\");
        fout.flush();
        fout.close();

        ProcessBuilder processBuilder = new ProcessBuilder("scriptDemo.cmd");
        processBuilder.redirectError(ProcessBuilder.Redirect.appendTo(new File("processbuilder_error.txt")));
        processBuilder.redirectOutput(new File("processbuilder_output.txt"));
        Process process = processBuilder.start();
        boolean successed = process.waitFor(4L, TimeUnit.SECONDS);
        process.destroy();
    }
}