1

in Java 8, windows 10, i have a text app, i want to open a console and write something there,

first try is:

    String [] cmd = {"cmd.exe", "/c", "start"};
    ProcessBuilder f = new ProcessBuilder(cmd);
    f.redirectErrorStream(true);
    Process p = f.start();
    PrintStream printStream=new PrintStream(p.getOutputStream());
    //
    System.setOut(printStream);
    System.out.println("this write in CMD"); //did not work 

second try is:

    printStream.println("this write in CMD");//did not work 

Can any body Help?

Ddll
  • 85
  • 8
  • The command `start` given to CMD.EXE causes your CMD.EXE to launch a new command window. That means you have 2 CMD.EXE processes and Java knows nothing of the second one. – DuncG Dec 15 '21 at 18:06

1 Answers1

0

Launch conhost.exe instead, and write to the stdin of the retained Process, or if you redirect your output stream to the process writes with System.out will appear in the new console window:

String [] cmd = {"conhost.exe"};
ProcessBuilder f = new ProcessBuilder(cmd);
f.redirectErrorStream(true);
Process p = f.start();
PrintStream printStream=new PrintStream(p.getOutputStream());
System.setOut(printStream);
System.out.println("dir");
DuncG
  • 12,137
  • 2
  • 21
  • 33