62,617
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
}
}
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;
}
}
}
}
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;
}
}
}
}