String[] cmd = { exe的路径, 参数 };
try {
final Process process = Runtime.getRuntime().exec(cmd);
// 处理InputStream的线程
new Thread() {
@Override
public void run() {
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
try {
while ((line = in.readLine()) != null) {
//System.out.println("output: " + line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
@Override
public void run() {
BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
try {
while ((line = err.readLine()) != null) {
System.out.println("err: " + line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
err.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
process.waitFor();
System.out.println("执行完毕");
} catch (Exception e) {
e.printStackTrace();
}
为了实现确保调用的exe程序执行完毕再执行其他函数,使用java progress的.waitfor()方法,但特别容易出现堵塞,上网查了下通过上述方法可以完美解决,算得上是一个小小的收获吧。