[英]start a process in a cmd window and get output
I want to start a process in a CMD window with Java, and the easiest way to do that is by 我想使用Java在CMD窗口中启动一个过程,而最简单的方法是通过
Runtime.getRuntime().exec("cmd /c start program.exe")
The problem is that now I can't get the input from the process. 问题在于,现在我无法从流程中获取输入。 How can I get the output from the process and be able to run it in a separate CMD window? 我如何从流程中获取输出并能够在单独的CMD窗口中运行它?
Your problem is that start
is a separate command whose purpose is to launch a completely new process unrelated to the cmd
that invokes start
. 您的问题是start
是一个单独的命令,其目的是启动一个与调用start
的cmd
不相关的全新进程。 Whatever start
then executes is not connected to the original cmd
and cannot be accessed by your Java program. 然后执行的start
均不连接到原始cmd
并且Java程序无法访问该cmd
。
If you need to access the subprocess' in/out/err streams, do not use start
. 如果您需要访问子流程的in / out / err流,请不要使用start
。
Hey bro if you want to println the output process of your process use this 嗨,兄弟,如果您想打印过程的输出过程使用此
Process process= Runtime.getRuntime().exec("cmd /c start program.exe");
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
line = br.readLine();
System.out.println(line);
}
with this you will get each output process exactly the same with cmd output. 这样,您将获得与cmd输出完全相同的每个输出过程。
if you want to process 2 cmd maybe you can make 2 process with different exec 如果您要处理2个cmd,也许可以用不同的exec进行2个处理
Process process1 = Runtime.getRuntime().exec("cmd /c start program1.exe");
Process process2 = Runtime.getRuntime().exec("cmd /c start program2.exe");
if you want this run with same thread please read java books about thread, you can run it at the same time with thread. 如果要在同一线程下运行,请阅读有关线程的Java书籍,您可以与线程同时运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.