我正在编写一个 java 程序,它将在 Linux Ubuntu 操作系统上运行一些已安装的工具,我需要在同一进程中执行两个连续的命令,但要使第一个命令完全执行,然后调用第二个命令。我曾尝试使用“\n”来分隔两个命令,如下面的代码所示,但它不起作用。
public static void main (String args[])
{
List<String> command = new ArrayList<String>();
command.add("spin");
command.add("-p");
command.add("-u100");
command.add("D:\\promela\\peterson.pml");
command.add("\n");
command.add("cc -o pan pan.c");
command.add("./pan");
String[] commandArray = command.toArray(new String[command.size()]);
ProcessBuilder pb = new ProcessBuilder(commandArray);
Process process;
try {
process = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errline, outline;
for(String s : command)
{
System.out.println(s + " ");
}
while ((outline = br.readLine()) != null)
{
System.out.println(outline);
}
while ((errline = errReader.readLine()) != null)
{
System.err.println(errline);
}
} catch (IOException e)
{
e.printStackTrace();
}
}