简体   繁体   中英

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

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?

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 . Whatever start then executes is not connected to the original cmd and cannot be accessed by your Java program.

If you need to access the subprocess' in/out/err streams, do not use 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.

if you want to process 2 cmd maybe you can make 2 process with different exec

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM