当从Java启动Chrome head head无头作为命令行申请过程时,如何正确地通过参数?

发布于 2025-01-20 15:35:59 字数 1232 浏览 1 评论 0原文

从命令行运行它时,它工作正常:(

"/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 额外的时间来加载网页的所有动态部分,然后再返回结果。)

当我 从 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 应用程序窗口。它不作为无头命令行应用程序运行,并且似乎看不到命令​​行参数。

我做错了什么?

When I run this from the command line it works fine:

"/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"

(Note: I'm using the 'virtual-time-budget' option to give Chrome extra time to load all dynamic portions of the web page before returning the result.)

When I run this simple 'ls' command from Java, it works fine and I see the output:

    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);
    }

But when I change the commands array like to this to run my original command ...

    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\""};

... it opens a blank Chrome application window. It doesn't run as a headless command line app, and does not appear to see the command line args.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2025-01-27 15:35:59

尝试将所有标记作为单独的数组元素:

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" };

Try all tokens as separate array elements:

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" };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文