0

How to start chrome using Java?

For Windows the code is just as simple as below.

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

Is there something like above?

2
  • And what do you want it to do if the user does not have Chrome installed? Commented Aug 13, 2017 at 12:57
  • Will catch the exception like nothing happened!. This is the first step. In the next step I will check on trying different browser if one fails. Commented Aug 13, 2017 at 13:09

4 Answers 4

5

In Linux you can use like this:

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

Replace the /path/to/chrome with the path in your system. Generally Google Chrome is installed at /opt/google/chrome/chrome

Or you can use google-chrome like this:

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

If you want to open up in chromium browser in Linux use it like this:

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

For MAC OS try like this:

Runtime.getRuntime().exec(new String[]{"/usr/bin/open", "-a", "/Applications/Google Chrome.app", "http://yourwebsite.com/"});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. Will try these.
0
You can use Selenium.

   import org.openqa.selenium.chrome.ChromeDriver;
public class App
{
    public static void main(String[] args) throws Throwable
    {
        ChromeDriver driver = new ChromeDriver();

        System.setProperty("webdriver.chrome.driver", "/usr/bin/google-chrome");

        // And now use this to visit Google
        driver.get("http://www.google.com");
}

Comments

0

this should work fine for ubuntu 15.10 or higher

    String[] cmd = {"bash","-c","google-chrome www.yourUrl.com"};
    Process p = Runtime.getRuntime().exec(cmd);

Hope it helps.

Comments

0
>>> Check here (Note: Java File is Compiled and run by commands on Terminal)  
     File name: Test.java  

     public class Test {  
          public static void main(String[] args) {  
             try {  
                   System.out.println("Executing command to open a chrome tab with terminal command!");  
                   Runtime.getRuntime().exec(new String[]{"bash", "-c", "google-chrome https://stackoverflow.com/"});  
                   System.out.println("A New tab or window should get opened in your Chrome Browser with Stack Overflow website!");  
                 } catch (Exception ex) {  
                     ex.printStackTrace();  
                 }  
          }  
     }  

/**here
     google-chrome https://stackoverflow.com/ -> terminal command to open a new chrome tab or window with the stack-overflow website.
**/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.