Close browser window using java code

后端 未结 3 989
离开以前
离开以前 2021-01-25 02:23

How should i close an opened browser window using java code. I have found a way to first find the process then end that process. Is there any better way? I have opened the brows

3条回答
  •  [愿得一人]
    2021-01-25 03:06

    You can put it in a Process and kill that.

    Runtime runtime = Runtime.getRuntime();
    Process p = runtime.exec("/usr/bin/firefox -new-window " + url);
    p.destroy();
    

    -- update --

    You should execute your command with a String array

    Process p = Runtime.getRuntime().exec(new String[]{
        "/usr/bin/firefox",
        "-new-window", url
    });
    

    This is less prone to errors: Java execute a command with a space in the pathname

    Or use ProcessBuilder: ProcessBuilder Documentation

提交回复
热议问题