简体   繁体   English

在“运行窗口”中调用命令

[英]Invoking a command in Run Window

Opening the Run window (Windows + r) and running a command -> I want to trigger this same command using Java. 打开运行窗口(Windows + r)并运行命令 - >我想使用Java触发相同的命令。 I tried this using : 我试过这个:

Runtime.getRuntime().exec(command);

But this did not worked. 但这没效果。 Please let me know how to achieve this. 请让我知道如何实现这一目标。

Can you try this: 你能试试这个:

ProcessBuilder pb=new ProcessBuilder("explorer");
        pb.redirectErrorStream(true);
        Process process=pb.start();
        BufferedReader inStreamReader = new BufferedReader(
            new InputStreamReader(process.getInputStream())); 

        while(inStreamReader.readLine() != null){
            //do something with commandline output.
        }

Use this command: 使用此命令:

Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "winword"});

This successfully runs Microsoft word ( winword ), which is not runnable directly through cmd. 这成功运行Microsoft word( winword ),它不能直接通过cmd运行。 The start command behaves the same as run does. start命令的行为与run do相同。

Add parameters afterwards like this: 之后添加参数如下:

Runtime.getRuntime().exec(new String[] {"cmd.exe", "/c", "start", "winword", "C:\\Example.docx"});

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

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