Home > Software design >  When launching Chrome headless as a command line application process from Java, how do I correctly p
When launching Chrome headless as a command line application process from Java, how do I correctly p

Time:04-12

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?

CodePudding user response:

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