使用 Java 完全重定向 CMD.exe 进程的输入/输出/错误流
我这个项目的目标是通过 Java 获得远程命令提示符的感觉。使用 TCP/IP 套接字,我的目标是在一台计算机上运行命令提示符进程,并将所有控制传输到另一侧。我立即偶然发现了 Runtime.getRuntime().exec() 和 Process 对象等。我已经解决了一半的问题。使用远程命令提示符,我可以运行单个命令,收集输出,然后将其发送回另一端。问题是,我似乎只能为每个命令提示符实例运行一个命令。这是行不通的(在我需要更改目录然后运行命令等的情况下)。我已经从这种情况中剥离了所有套接字/网络编程来向您展示(并为我创建一个更简单的测试环境)。
import java.io.*;
public class testingProgram {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
StringBuilder inputMessage = new StringBuilder();
String resultData;
try {
Process pr = rt.exec("cmd.exe /c net user");
BufferedReader processInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedReader errorProcessInput = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
PrintWriter processOut = new PrintWriter(pr.getOutputStream());
while( (resultData = processInput.readLine()) != null ) {
inputMessage.append(resultData + "\n");
}
resultData = inputMessage.toString();
System.out.print(resultData);
} catch(IOException e) {
} //catch(InterruptedException e) {
//}
}
}
我还有很多,但这就是我的问题所在。我可以使用来自套接字流的简单变量和消息自定义命令“net user”,所以这不是我的问题。我的问题是我需要创建一个持续的命令提示符实例,保留输入/输出的所有重定向。基本上,我希望能够在“net user”之后发送另一个命令。
我已经收集并重定向了输出流。我希望能够做类似的事情:
processOut.write("net user");
我希望能够使用它,让命令提示符运行命令,并保留输出(无论是来自 errorStream 还是 inputStream)。
我只是需要更多关于如何去做这件事的指导。
My aim with this project was to have a remote command prompt feel with Java. Using TCP/IP sockets, I was aiming to run a command prompt process on one computer, and virtually transmit all control to the other side. I immediately stumbled over Runtime.getRuntime().exec() and Process objects, etc. I've solved my problem about halfway. With my remote command prompt, I can run a single command, gather the output, and send it back to the other side. The problem is, I can only seem to run one command per command prompt instance. This won't do (with situations where I need to change directory and THEN run a command, etc). I've stripped all socket/networking programming from this situation to show you (and to create an easier testing environment for me).
import java.io.*;
public class testingProgram {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
StringBuilder inputMessage = new StringBuilder();
String resultData;
try {
Process pr = rt.exec("cmd.exe /c net user");
BufferedReader processInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedReader errorProcessInput = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
PrintWriter processOut = new PrintWriter(pr.getOutputStream());
while( (resultData = processInput.readLine()) != null ) {
inputMessage.append(resultData + "\n");
}
resultData = inputMessage.toString();
System.out.print(resultData);
} catch(IOException e) {
} //catch(InterruptedException e) {
//}
}
}
I have a lot more, but this is where my problem is. I can customize the command "net user" with a simple variable and message from the socketstream, so that's not my problem. My problem is that I need to create an ongoing command prompt instance, retaining all redirections of the input/output. Basically, I would like to be able to send another command AFTER "net user".
I have gathered and redirected the output stream. I want to be able to do something like:
processOut.write("net user");
I want to be able to use this, have the command prompt run the command, and retain the output (whether it be from the errorStream OR the inputStream).
I just need some more direction on how to go about doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该研究多线程。您基本上想要的是一个持续运行并维护 rt 的线程。
像这样:
有关多线程的进一步参考:
http://download.oracle.com/javase/tutorial/essential/concurrency /procthread.html
您的问题是您的程序在一次调用后终止。
干杯
You should look into multi threading. What you basically want is a thread which keeps running and maintaining the rt.
Like this:
For further reference on multithreading:
http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.html
You problem is that your program terminates after one call.
cheers
您告诉命令解释器终止。删除cmd.exe后面的/C。
You're telling the command interpreter to terminate. Remove the /C after cmd.exe.