执行"码头历史记录[id] -q"在终端中,您将获得一个id字符串列表。
现在我想通过java程序获取字符串列表。但是,有时我只能获得输出的第一行。 (当我反复运行相同的命令时,这会随机发生。)
代码如下所示,有人对此有所了解吗?非常感谢你!
String[] cmdParaArray = {"docker", "history", ID, "-q"};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CmdExecutor.execute(cmdParaArray, baos);
String imageInfoJsonString = baos.toString();
public static List<String> execute(String[] cmdParaArray, OutputStream os) {
try {
ProcessBuilder pb = new ProcessBuilder(cmdParaArray)
.redirectErrorStream(true);
Process proc = pb.start();
InputStream is = proc.getInputStream();
BufferedReader reader
= new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
if (os != null) {
os.write((line + "\n").getBytes());
os.flush();
}
}
}