[英]Launch CMD command with spaces with JAVA
I want to excute in CMD with ProcessBuilder and here's my code : 我想使用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();
But I keep getting : 但我不断得到:
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
And I'm sure about chrome.exe and template2.html that they're in there respective paths. 而且我对chrome.exe和template2.html确信它们位于各自的路径中。
EDIT 编辑
I also tried this : 我也试过这个:
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");
And I got : 我得到了:
windows can't find --headless
*EDIT 2 * *编辑2 *
I tried this command : 我尝试了以下命令:
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();
And I got : 我得到了:
Cannot run program "cd "Program Files (x86)"": CreateProcess error=2, File not found
To run a program with cmd you only need cmd /c %programname%
, but you added start
which caused the problem. 要使用cmd运行程序,您只需要cmd /c %programname%
,但是您添加了start
引起了问题。
Change your ProcessBuilder to: 将您的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.