简体   繁体   English

使用java将输出写入命令提示符

[英]write output to command prompt using java

I am trying to open a command prompt from a java class and send output to the new command prompt. 我试图从java类打开命令提示符并将输出发送到新的命令提示符。 I can open cmd using process . 我可以使用process打开cmd Gone through all the answers on SO,but couldn't figure out how can I pass the output to the just created cmd window. 浏览了SO上的所有答案,但无法弄清楚如何将输出传递给刚刚创建的cmd窗口。

This might not be practical or even usable, 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 a CMD and write (a bit too literally maybe) "Hello" in the CMD. 这将打开CMD并在CMD中写入(有点太字面意思)“Hello”。

See this answer if you want to type a String. 如果要键入字符串,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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