使用 java 手动运行命令(使用 cmd.exe)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17956168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 19:42:49  来源:igfitidea点击:

Running command manually with java (using cmd.exe)

javacmdcd

提问by austinthemassive

So the following opens a new browser window when I put it in cmd manually:

因此,当我手动将其放入 cmd 时,以下内容会打开一个新的浏览器窗口:

cd C:/Program Files (x86)/Google/Chrome/Application&chrome.exe

However, when I tried to do this via a java program (see below), the command prompt opens and goes to the correct directory, but no new window opens. Any ideas of what I need to change?

但是,当我尝试通过 Java 程序(见下文)执行此操作时,命令提示符会打开并转到正确的目录,但没有打开新窗口。我需要改变什么的任何想法?

    Runtime rt = Runtime.getRuntime();
    rt.exec("cmd.exe /c start cd C:/Program Files (x86)/Google/Chrome/Application&chrome.exe");

回答by dic19

Try ProcessBuilderinstead of Runtime:

尝试ProcessBuilder代替Runtime

String command = "C:/Program Files (x86)/Google/Chrome/Application&chrome.exe";
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", command);
Process p = pb.start();

See also:

也可以看看:

回答by coding_idiot

rt.exec("cmd.exe /c start cd \"C:/Program Files (x86)/Google/Chrome/Application&chrome.exe\"");

rt.exec("cmd.exe /c start cd \"C:/Program Files (x86)/Google/Chrome/Application&chrome.exe\"");

Not tested but this shall work, I just put the complete path in double quotes so that because of spaces it's not considered to be the next argument.

未经测试,但这应该有效,我只是将完整路径放在双引号中,以便由于空格而不会将其视为下一个参数。

If that doesn't work, I suggest trying Apache Commons Execlibrary, because I always use that.

如果这不起作用,我建议尝试使用Apache Commons Exec库,因为我总是使用它。

Here is some sample code from one of my applications :

这是我的一个应用程序中的一些示例代码:

CommandLine cmdLine = new CommandLine("cmd.exe");
                    cmdLine.addArgument("/c");
                    cmdLine.addArgument(".\phantomjs\nk\batchbin\casperjs.bat");
                    cmdLine.addArgument(".\phantomjs\nk\batchbin\dd.js");
                    cmdLine.addArgument(url);
                    cmdLine.addArgument(">" + rand);
                    DefaultExecutor executor = new DefaultExecutor();
                    int exitValue = executor.execute(cmdLine);

Using something like above the complete path to chrome.exe should be added as a new argument, and then the library will take care of escaping.

使用上面类似的内容,应将 chrome.exe 的完整路径添加为新参数,然后库将负责转义。

回答by Benjamin James

I run a chrome process using:

我使用以下方法运行 chrome 进程:

            ProcessBuilder builder = new ProcessBuilder();            
            builder.command("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "https://your url");            
            Process process = builder.start();
            int exitCode = process.waitFor();        
            assert exitCode == 0;