I am making a program to automatically test a CLI. The one part that I am getting stuck at is reading in the output from the command line. My code looks like this:
private static File createTempScript(List<String> commands) throws IOException {
final File tempScript = File.createTempFile("script", null);
final Writer streamWriter = new OutputStreamWriter(new FileOutputStream(
tempScript));
final PrintWriter printWriter = new PrintWriter(streamWriter);
for(String command: commands) {
printWriter.println(command);
}
printWriter.close();
return tempScript;
}
/**
* Takes in a list of commands that then get sent to make a script. After the script is created this method will
* then run the script.
*/
public void executeCommand(List<String> commands) throws IOException {
final File tempScript = createTempScript(commands);
final ProcessBuilder pb = new ProcessBuilder("bash", tempScript.toString());
final Process process = pb.start();
final BufferedReader in = new BufferedReader(new
InputStreamReader(process.getInputStream()));
final BufferedReader out = new BufferedReader(new
InputStreamReader(process.getInputStream()));
String s;
while ((s = in.readLine()) != null) {
if(!s.isEmpty())
{
Output.add(s);
}
}
}
The problem I am having now is that in the while loop whenever it gets to a line that I need to input in a command the program just hangs. Whenever I comment out that loop the program works fine.
Thanks for the help.
Aucun commentaire:
Enregistrer un commentaire