我寫這將在Linux的Ubuntu操作系統上運行一些安裝工具Java程序的輸入按鍵到命令行,我需要在相同的方法,只是使執行兩個連續命令第一個完全執行,然後調用第二個。我曾嘗試使用「\ n」來分隔兩個命令,如下面的代碼所示,但它不起作用。如何通過從Java應用程序
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();
}
}
@MarcoForberg沒有,我怎麼可以通過CTRL +從Java進入? – Sami
啊忘了吧。錯了。以爲你需要在從控制檯讀取時通過輸入。 –