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)