在老外的网站上找到一个类可以实现:
import java.io.*;
/**
*Description of the Class
*
* @author Administrator
* @created23 February 2004
*/
public class RunAsync {
/**
*Description of the Method
*
* @paramargsDescription of the Parameter
*/
public static void main(String args[]) {
try {
if (args.length "
;
System.exit(-1);
}
Process pro = null;
if (args.length > 1) {
pro = Runtime.getRuntime().exec(args);
}
else {
pro = Runtime.getRuntime().exec(args[0]);
}
InputStream error = pro.getErrorStream();
InputStream output = pro.getInputStream();
Thread err = new Thread(new OutErrReader(error));
Thread out = new Thread(new OutErrReader(output));
out.start();
err.start();
pro.waitFor();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
catch (java.lang.InterruptedException e) {
e.printStackTrace();
}
}
/**
*Description of the Class
*
* @author Administrator
* @created23 February 2004
*/
static class OutErrReader implements Runnable {
InputStream is;
/**
*Constructor for the OutErrReader object
*
* @paramisDescription of the Parameter
*/
public OutErrReader(InputStream is) {
this.is = is;
}
/**
*Main processing method for the OutErrReader object
*/
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(is));