有我的問題,我有一個.BAT誰不得不做的事:爪哇 - 調用Runtime.getRuntime()EXEC()關鍵事件
@echo off
echo "Hello world!"
pause
我的回答是,有沒有辦法趕上事件當Java程序使用 Runtime.getRuntime()。exec(FullCommand)「Press Any Key」(來自外部程序)?
在我的Java代碼中,我使用所有的通量(的InputStream,OutputStream中,errorstream,到單獨的線程) 和我已經tryed趕上了的BufferedReader行,但「按任意鍵」不來,如果我不使用我的輸出流模擬按鍵輸入與newLine();,因爲過程不想完成。
所以我想知道是否有一個技巧知道什麼時候進程等待或者是否有辦法在輸入key方法之前捕獲輸入流「按任意鍵」之後使用outputstream方法(因爲我不'我想運行所有腳本都使用這種方法)。
1)
Run Exec
proc = Runtime.getRuntime().exec(FullCommand);
try {
Ti = new Thread(new DisplayInputStream(proc.getInputStream()));
Te = new Thread(new DisplayDebugStream(proc.getErrorStream()));
Ti.start();
Te.start();
proc.waitFor();
Ti.join();
Te.join();
}
......
2)
InputStream
方法
BufferedReader br = getBufferedReader(inputStream);
setInfosProcess(null);
try {
while ((StrInprocess = br.readLine()) != null) {
if(???) //Condition Press Event key
{
Thread To;
To = new Thread(new DisplayOutputStream(proc.getOutputStream()));
To.start();
}
if (getInfosProcess() == null) {
setInfosProcess(StrInprocess + "\n");
} else {
setInfosProcess(getInfosProcess() + StrInprocess + "\n");
}
}
}
......
3)
OutputStream
方法
BufferedWriter bwr = getBufferedWriter(outputStream);
setOutputProcess(null);
try {
bwr.newLine();// Simulate pressing of the Enter key
bwr.flush();// Flush the stream, otherwise it doesn't work
}
......
調試
run:
Hello world! Test:-1 //To compare string with what i want
//"Press Any Key" from pause don't come
//infinite loop caused by **pause**
泰的任何幫助。
嗨,問題在於進程等待按鍵來繼續他必須做的事情(在這種情況下退出),因此緩衝區在我使用輸出方法之前無法讀取暫停的「按任意鍵」,並且這真的很奇怪。 – Adhin37
你試過我的回答嗎?你還沒有得到'Press Any Key'文本? – Codebender
是的還是一樣的。 – Adhin37