【发布时间】:2017-12-12 10:10:20
【问题描述】:
我想用 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 /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"吗? -
我刚试了一下,结果一样
标签: java cmd processbuilder