Richie
Richie

Reputation: 5199

How to launch chrome browser from java

Is there any smart way to launch the chrome browser from a java class? I'm asking because I would like a smart way to launch an application that required a chrome browser on a machine that has internet explorer as a default browser and java 1.4.2 installed.

thanks

Upvotes: 6

Views: 29935

Answers (3)

Bayron Vazquez
Bayron Vazquez

Reputation: 371

This is the best way I found for this problem, since it does not matter where the browser is installed or whatever, java communicates with the operating system and makes a request to run the default browser.

String url = "https://yourdomain.org/";
java.awt.Desktop.getDesktop().browse( java.net.URI.create(url));

Upvotes: 3

sendon1982
sendon1982

Reputation: 11314

You can try Selenium Here:

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

}

Add Maven Dependency:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.42.2</version>
    </dependency>

Upvotes: 3

bdunn
bdunn

Reputation: 482

You can execute chrome.exe like this:

try {
    Process p = Runtime.getRuntime().exec("\"/Program Files (x86)/Google/Chrome/Application/chrome.exe\"");
    p.waitFor();
    System.out.println("Google Chrome launched!");
} catch (Exception e) {
    e.printStackTrace();
}

Provided you know where Chrome is installed.

Upvotes: 9

Related Questions