从 Java 以命令行应用程序进程无头启动 Chrome 时,如何正确传递参数?
When launching Chrome headless as a command line application process from Java, how do I correctly pass arguments in?
当我从命令行 运行 时,它工作正常:
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --virtual-time-budget=5000 --dump-dom "https://www.kijiji.ca"
(注意:我使用 'virtual-time-budget' 选项给 Chrome 额外的时间来在返回结果之前加载网页的所有动态部分。)
当我从 Java 运行 这个简单的 'ls' 命令时,它工作正常,我看到了输出:
Runtime rt = Runtime.getRuntime();
String[] commands = {"ls", "-lah"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
// Read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
但是当我将 commands
数组更改为 运行 我的原始命令时...
String[] commands = {"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"--headless --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --virtual-time-budget=5000 --dump-dom \"https://www.kijiji.ca\""};
... 它会打开一个空白 Chrome 应用程序 window。它不是 运行 作为无头命令行应用程序,并且似乎看不到命令行参数。
我做错了什么?
尝试将所有标记作为单独的数组元素:
String[] cmd = { "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--headless", "--disable-gpu", "--no-sandbox", "--run-all-compositor-stages-before-draw", "--virtual-time-budget=5000", "--dump-dom", "https://www.kijiji.ca" };
当我从命令行 运行 时,它工作正常:
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --virtual-time-budget=5000 --dump-dom "https://www.kijiji.ca"
(注意:我使用 'virtual-time-budget' 选项给 Chrome 额外的时间来在返回结果之前加载网页的所有动态部分。)
当我从 Java 运行 这个简单的 'ls' 命令时,它工作正常,我看到了输出:
Runtime rt = Runtime.getRuntime();
String[] commands = {"ls", "-lah"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
// Read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
但是当我将 commands
数组更改为 运行 我的原始命令时...
String[] commands = {"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"--headless --disable-gpu --no-sandbox --run-all-compositor-stages-before-draw --virtual-time-budget=5000 --dump-dom \"https://www.kijiji.ca\""};
... 它会打开一个空白 Chrome 应用程序 window。它不是 运行 作为无头命令行应用程序,并且似乎看不到命令行参数。
我做错了什么?
尝试将所有标记作为单独的数组元素:
String[] cmd = { "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", "--headless", "--disable-gpu", "--no-sandbox", "--run-all-compositor-stages-before-draw", "--virtual-time-budget=5000", "--dump-dom", "https://www.kijiji.ca" };