简体   繁体   中英

How do I automate a command line script with Java ProcessBuilder

I'm relative newbie to java and am trying to automate command line using java. I tried to search for the solution here, but couldn't find it.

I created a simple test shell script like below for testing my program:


#!/bin/bash
echo "What is your name?";
read name;
echo "Hello, $name"
echo "What is your contact number?";
read num;
echo "Saved contact number $num for $name"

The Java code is below:

import java.io.*;
import java.util.*;

public class CmdLineMain {
    public static void main(String args[]) throws InterruptedException, IOException {
        List<String> command = new ArrayList<String>();
        command.add("./test.sh");

        ProcessBuilder builder = new ProcessBuilder(command);

        final Process process = builder.start();
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        BufferedWriter bw = null;

        while (process.isAlive()) {
            line = br.readLine();
            // since stream may be closed earlier, re-open it
            bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
            System.out.println(line);

            if (line != null) {
                switch (line) {
                case "What is your name?":
                    bw.write("John Doe");
                    bw.close();
                    break;
                case "What is your contact number?":
                    bw.write("123456789");
                    bw.close();
                    break;

                }
            }

        }

        System.out.println("Program terminated!");
    }
}

Problem: The second input to the process fails with error:


    What is your name?
    Hello, John Doe
    What is your contact number?
    Exception in thread "main" java.io.IOException: Stream Closed
        at java.io.FileOutputStream.writeBytes(Native Method)
        at java.io.FileOutputStream.write(FileOutputStream.java:326)
        at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
        at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
        at java.io.FilterOutputStream.close(FilterOutputStream.java:158)
        at sun.nio.cs.StreamEncoder.implClose(StreamEncoder.java:320)
        at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:149)
        at java.io.OutputStreamWriter.close(OutputStreamWriter.java:233)
        at java.io.BufferedWriter.close(BufferedWriter.java:266)
        at nkh.app.CmdLineMain.main(CmdLineMain.java:34)

Closing the BufferedWriter will close its underlying streams, this includes the process OutputStream which you get through process.getOutputStream() . Thus, once it is closed in one loop, in the next loops you have your BufferedWriter wrapping a closed stream. Instead wrap the output stream only once and reuse that.

Like this:

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
// while the stream is open and there is something to read
// probably a better condition than `process.isAlive()`
while ((line = br.readLine()) != null) {
    switch (line) {
        case "What is your name?":
            bw.write("John Doe");
            bw.newLine();
            bw.flush();
            break;
        case "What is your contact number?":
            bw.write("123456789");
            bw.newLine();
            bw.flush();
            break;
    }
}

The reason your original code died on close() rather than on the write() that came before it when your new BufferedWriter was wrapping the closed underlying stream was because BufferedOutputStream.write() may not actually write to the underlying stream yet, since it's buffered. Calling flush() should tell the stream to actually write, and as you see in the stack trace close() is calling flush() which is ultimately writing the buffered bytes to the underlying FileOutputStream , which is then realizing that the FileOutputStream is already closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM