我运行下面附带的代码,但只有在字符串响应中完成命令后才会输出。我希望在命令运行期间激活控制台窗口并输出命令。
String command = "src\\youtube-dl"
+ " --max-downloads " + maxdown.getText()
+ " --retries " + noofretry.getText()
+ " --write-all-thumbnails" + sub.getText()
+ " " + url.getText();
String response = "";
try {
Process p = Runtime.getRuntime().exec(command);
InputStream in = p.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c = -1;
while((c = in.read()) != -1) {
baos.write(c);
System.out.flush();
response = response + new String(baos.toByteArray());
System.out.println(response);
}
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:0)
请尝试使用waitFor方法,直到将响应正确刷新到输出流
Process p = null;
try {
p = p = r.exec(cmd2);
p.getOutputStream().close(); // close stdin of child
InputStream processStdOutput = p.getInputStream();
Reader r = new InputStreamReader(processStdOutput);
BufferedReader br = new BufferedReader(r);
String line;
while ((line = br.readLine()) != null) {
//System.out.println(line); // the output is here
}
p.waitFor();
}
catch (InterruptedException e) {
...
}
catch (IOException e){
...
}
finally{
if (p != null)
p.destroy();
}
价: