在老外的网站上找到一个类可以实现:
import java.io.*;
/**
* Description of the Class
*
* @author Administrator
* @created 23 February 2004
*/
public class RunAsync {
/**
* Description of the Method
*
* @param args Description of the Parameter
*/
public static void main(String args[]) {
try {
if (args.length < 1) {
System.out.println("Usage: java RunAsync "
;
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
* @created 23 February 2004
*/
static class OutErrReader implements Runnable {
InputStream is;
/**
*Constructor for the OutErrReader object
*
* @param is Description 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));
String temp = null;
while ((temp = in.readLine()) != null) {
// System.out.println(temp);
Frame1.jTextArea1.append("\n"
;
Frame1.jTextArea1.append(temp);
}
is.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
我稍加修改了一下:
package oraclem;
import java.io.*;
public class RunCmd{
public static void run(String s){
try{ Process pro=Runtime.getRuntime().exec(s);
InputStream error=pro.getErrorStream();
InputStream output = pro.getInputStream();
NewThread err=new NewThread(error);
NewThread out=new NewThread(output);
err.start();
out.start();
pro.waitFor();
}catch (java.io.IOException e) {
e.printStackTrace();
}
catch (java.lang.InterruptedException e) {
e.printStackTrace();
}
}
}
class NewThread extends Thread{
InputStream is;
public NewThread(InputStream is){
this.is=is;}
public void run(){
try{BufferedReader in = new BufferedReader(new InputStreamReader(is));
String temp = null;
while ((temp = in.readLine()) != null) {
// System.out.println(temp);
Frame1.jTextArea1.append("\n"
;
Frame1.jTextArea1.append(temp);
}
}catch (Exception e) {
e.printStackTrace();}
}
}