在 Linux 和 MAC 中使用 Java 在 Chrome 中打开 URL

Sha*_*P S 0 java linux macos command-line google-chrome

如何使用 Java 启动 chrome?

对于 Windows,代码就像下面一样简单。

Runtime.getRuntime().exec(new String[]{"cmd", "/c", "start chrome http://localhost:8080"});
Run Code Online (Sandbox Code Playgroud)

有没有类似上面的?

Ans*_*Raj 5

在 Linux 中,您可以像这样使用:

Runtime.getRuntime().exec(new String[]{"bash", "-c", "/path/to/chrome http://yourwebsite.com"});
Run Code Online (Sandbox Code Playgroud)

将 /path/to/chrome 替换为您系统中的路径。通常 Google Chrome 安装在 /opt/google/chrome/chrome

或者你可以像这样使用谷歌浏览器:

Runtime.getRuntime().exec(new String[]{"bash", "-c", "google-chrome http://yourwebsite.com"});
Run Code Online (Sandbox Code Playgroud)

如果你想在 Linux 中的 Chrome 浏览器中打开,像这样使用它:

Runtime.getRuntime().exec(new String[]{"bash", "-c", "chromium-browser http://yourwebsite.com"});
Run Code Online (Sandbox Code Playgroud)

对于 MAC 操作系统,请尝试这样:

Runtime.getRuntime().exec(new String[]{"/usr/bin/open", "-a", "/Applications/Google Chrome.app", "http://yourwebsite.com/"});
Run Code Online (Sandbox Code Playgroud)