繁体   English   中英

使用JAVA使用空格启动CMD命令

[英]Launch CMD command with spaces with JAVA

我想使用ProcessBuilder在CMD中执行命令,这是我的代码:

ProcessBuilder pb = new ProcessBuilder(
            "cmd /c start C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe --headless --disable-gpu --print-to-pdf javaGen.pdf  file:///C:/Users/User/Desktop/template/template2.html");
pb.start();

但我不断得到:

Cannot run program "cmd /c start C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --headless --disable-gpu --print-to-pdf javaGen.pdf  file:///C:/Users/User/Desktop/template/template2.html": CreateProcess error=2, file not found

而且我对chrome.exe和template2.html确信它们位于各自的路径中。

编辑

我也试过这个:

Process p = Runtime.getRuntime().exec(
            "cmd /c start \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" --headless --disable-gpu --print-to-pdf javaGen.pdf  file:///C:/Users/User/Desktop/template/template2.html");

我得到了:

windows can't find --headless

*编辑2 *

我尝试了以下命令:

pb.command(new String[] { "cd \"Program Files (x86)\"", "cd Google\\Chrome\\Application\\",
            "chrome.exe --headless --disable-gpu --print-to-pdf javaGen.pdf  file:///C:/Users/User/Desktop/template/template2.html" });
    pb.start();

我得到了:

Cannot run program "cd "Program Files (x86)"": CreateProcess error=2, File not found

要使用cmd运行程序,您只需要cmd /c %programname% ,但是您添加了start引起了问题。

将您的ProcessBuilder更改为:

ProcessBuilder pb = new ProcessBuilder(
            "cmd", "/c", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "--headless", "--disable-gpu", "--print-to-pdf", "javaGen.pdf",  "file:///C:/Users/User/Desktop/template/template2.html");
pb.start();

暂无
暂无

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

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