Write output on the command line using java

I am trying to open a command line from a java class and send the output to a new command line. I can open cmdwith process. All answers to SO went through, but could not understand how to transfer the result to the window just created cmd.

+4
source share
1 answer

It may be impractical or even useful, but it does the job:

String[] command = {"cmd", "/c", "start", "cmd.exe"};
try {
    new ProcessBuilder(command).start();
    Robot r = new Robot();

    r.keyPress(KeyEvent.VK_SHIFT);
    r.keyPress(KeyEvent.VK_H);
    r.keyRelease(KeyEvent.VK_H);
    r.keyRelease(KeyEvent.VK_SHIFT);
    r.keyPress(KeyEvent.VK_E);
    r.keyRelease(KeyEvent.VK_E);
    r.keyPress(KeyEvent.VK_L);
    r.keyRelease(KeyEvent.VK_L);
    r.keyPress(KeyEvent.VK_L);
    r.keyRelease(KeyEvent.VK_L);
    r.keyPress(KeyEvent.VK_O);
    r.keyRelease(KeyEvent.VK_O);
} catch (IOException | AWTException e) {
    e.printStackTrace();
}

This opens the CMD and writes (perhaps too literally) “Hello” to the CMD.

See this answer if you want to type a string.

+4
source

Source: https://habr.com/ru/post/1657987/


All Articles