关于process的问题,求请教

moao_l 2013-01-07 08:07:06
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFrame;
/***
* @author bin
*/
public class MyCmd extends JFrame {
private static final long serialVersionUID = 1L;
private Process process;
private static PrintWriter out;
private static String command;
public MyCmd() {
try {
this.process = Runtime.getRuntime().exec("cmd");
out = new PrintWriter(process.getOutputStream());
new ConsoleIntercepter(process.getInputStream()).start();
new ConsoleIntercepter(process.getErrorStream()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception, Exception,
Exception, Exception {
new MyCmd();
while (true) {
Scanner input = new Scanner(System.in);
command = input.nextLine();
if (command.equals(""))
return;
out.println(command);
out.flush();
}
}
}

class ConsoleIntercepter extends Thread {
private InputStream is;

public ConsoleIntercepter(InputStream is) {
this.is = is;
}
@Override
public void run() {
byte[] buf = new byte[1024];
int size;
while (true) {
try {
while ((size = is.read(buf)) != -1) {
System.out.print(new String(buf, 0, size, "gbk"));
}
} catch (IOException e) {
e.printStackTrace();
break;
}
}
}
}


没看明白为何程序会一直往下执行。
当构造方法执行后开启了两个线程,但之后并未再执行Runtime.getRuntime().exec("cmd");啊,
怎么会返回cmd里面的内容呢?
...全文
231 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
moao_l 2013-01-10
  • 打赏
  • 举报
回复
引用 2 楼 abc41106 的回复:
贴一个在你的程序基础上修改了一点的。 Java code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283……
谢谢了
abc41106 2013-01-07
  • 打赏
  • 举报
回复
贴一个在你的程序基础上修改了一点的。
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFrame;

/***
 * @author bin
 */
public class MyCmd extends JFrame {
	private static final long serialVersionUID = 1L;
	private Process process;
	private static PrintWriter out;
	private static String command;
	private ConsoleIntercepter iproc;
	private ConsoleIntercepter eproc;

	public MyCmd() {
		try {
			this.process = Runtime.getRuntime().exec("cmd");
			out = new PrintWriter(process.getOutputStream());
			iproc = new ConsoleIntercepter(process.getInputStream());
			eproc = new ConsoleIntercepter(process.getErrorStream());
			iproc.start();
			eproc.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void stopProc(){
		iproc.shutDownThread();
		eproc.shutDownThread();
	}

	public static void main(String[] args) throws Exception, Exception,
			Exception, Exception {
		MyCmd mc = new MyCmd();
		while (true) {
			Scanner input = new Scanner(System.in);
			command = input.nextLine();
			if (command.equals("")) {
				mc.stopProc();
				input.close();
				out.close();
				return;
			}
			out.println(command);
			out.flush();
		}
	}
}

class ConsoleIntercepter extends Thread {
	private InputStream is;
	private boolean flag = true;

	public ConsoleIntercepter(InputStream is) {
		this.is = is;
	}

	public synchronized void shutDownThread() {
		flag = false;
	}

	public synchronized boolean isShutDown() {
		return flag;
	}

	public void run() {
		byte[] buf = new byte[1024];
		int size;
		while (isShutDown()) {
			try {
				while ((size = is.read(buf)) != -1) {
					System.out.print(new String(buf, 0, size, "gbk"));
				}
			} catch (IOException e) {
				e.printStackTrace();
				break;
			}
		}
	}

}
abc41106 2013-01-07
  • 打赏
  • 举报
回复
一直在运行的原因是你的线程里面用的是while(true),线程没有结束 另一个原因是没有关闭流
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JFrame;

/***
 * @author bin
 */
public class MyCmd extends JFrame {
	private static final long serialVersionUID = 1L;
	private Process process;
	private static PrintWriter out;
	private static String command;

	public MyCmd() {
		try {
			this.process = Runtime.getRuntime().exec("cmd");
			out = new PrintWriter(process.getOutputStream());
			new ConsoleIntercepter(process.getInputStream()).start();
			new ConsoleIntercepter(process.getErrorStream()).start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception, Exception,
			Exception, Exception {
		new MyCmd();
		while (true) {
			Scanner input = new Scanner(System.in);
			command = input.nextLine();//这里获取你的输入
			if (command.equals("")){
				return;
			out.println(command);//这里将你的输入输出到cmd,即作为cmd输入
			out.flush();
		}
	}
}

class ConsoleIntercepter extends Thread {
	private InputStream is;

	public ConsoleIntercepter(InputStream is) {
		this.is = is;
	}

	public void run() {
		byte[] buf = new byte[1024];
		int size;
		while (true) {
			try {
				while ((size = is.read(buf)) != -1) {//这里获取cmd输出
					System.out.print(new String(buf, 0, size, "gbk"));//这里打印出来
				}
			} catch (IOException e) {
				e.printStackTrace();
				break;
			}
		}
	}
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧