Reputation: 105
I'm working on reading the output of a script that is invoked using a Java process. However, in the middle of the script run, it will in SOME situations prompt the user to answer y/n to continue. However, after reading many posts on StackOverflow, I'm still stuck with detecting the prompt and then sending the response while the process is still running.
If anyone has any ideas, that would be awesome.
I've tried reading from Scanner class and System.console to no prevail.
Here is a portion of the code I'm using.
Process p;
String file = "./upgrade.sh";
cmds.add(file);
cmds.add(sourcePath);
cmds.add(outputDirectoryPath);
cmds.add(zip);
cmds.add("-c");
//}
pb = new ProcessBuilder(cmds);
pb.directory(new File(binDir));
p = pb.start();
BufferedReader reader = new BufferedReader (new InputStreamReader(p.getInputStream()));
BufferedReader reader2 = new BufferedReader (new InputStreamReader(p.getErrorStream()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
try
{
String line = reader.readLine();
while (line != null)
{
System.out.println(line);
line = reader.readLine();
}
reader.close();
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
p.destroy();
Upvotes: 0
Views: 643
Reputation: 121780
The problem is that you use a BufferedReader
. It will only return when it has read a full line, that is a line separator. But if the script asks for something with a prompt, there won't be a line separator! As a result it WILL NOT return.
You have to use some other kind of reader in order to control the process.
Upvotes: 1