【发布时间】:2015-09-14 10:33:47
【问题描述】:
这是我的问题,我有一个必须做的 .bat :
@echo off
echo "Hello world!"
pause
我的回答是,当 java 程序使用 Runtime.getRuntime().exec(FullCommand )?
在我的 java 代码中,我使用所有通量(输入流、输出流、错误流,进入单独的线程) 我已经尝试使用 BufferedReader 来接听电话,但是如果我不使用输出流来模拟按键 Enter,则不会出现“Press Any Key” newLine();,因为进程不想结束。
所以我想知道是否有一个技巧可以知道进程何时等待,或者是否有办法在输入键方法之前捕获输入流“按任意键”,然后使用输出流方法(因为我不想对我运行的所有脚本使用此方法)。
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**
请帮忙。
【问题讨论】:
标签: java multithreading events runtime