Java Program to Open the given URL in System Default Browser in Windows


There are different ways of viewing web pages in a browser using URL. Here, the methods of doing the same are specified using the Java code. The given URL is first entered by using the Java program. Then the related Webpage is opened in the default browser. This article uses three different approaches for opening the webpage as specified by the URL in the browser through the Java code.

Multiple Approaches

For these programs, the displaying of the given URL is done using two different approaches.

  • By Using desktop.browse(uri) for the object belonging to Desktop class.

  • By Using javafx libraries and getHostServices().showDocument(theURL).

Algorithm

  • Step 1 − Specify the URL and give URL as a String.

  • Step 2 − Import the required libraries.

  • Step 3 − Open the given URL by using Java Functions or Methods.

  • Step 4 − Display the webpage in the default browser as specifies by the URL

Let’s see the program along with its output one by one.

Approach-1: By Using desktop.browse(uri).

In this approach, the Desktop class supports the launch of the user’s default browser to show a specified URI . In example 1, the URL is kept as fixed string.

Example

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.awt.Desktop;
public class openuri{
   
   //Java Program to Open the given URL in System Default Browser in Windows
   public static void main(String[] args) {
      if (Desktop.isDesktopSupported()) {
      
         //making a desktop object
         Desktop desktop = Desktop.getDesktop();
         try {
            URI uri = new URI("https://www.tutorialspoint.com/index.htm");
            desktop.browse(uri);
         } catch (IOException excp) {
            excp.printStackTrace();
         } catch (URISyntaxException excp) {
            excp.printStackTrace();
         }
      }
   }
}

Output

C:\java\javaprgstu>javac openuri.java
C:\java\javaprgstu>java openuri

Approach 2: By Using javafx libraries and getHostServices(). showDocument (theURL).

In this approach, the JavaFX Scene class is the main container for holding the content.and VBox is a layout that puts the components in it in a single vertical column. Here first a button is made. Then it is contained in a VBox. Then this VBox holding the button is put in the scene. The URI is fetched on clicking that button.

Example (Approach 2)

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.layout.VBox;

//Java Program to Open the specified URL in System Default Browser in Windows on press of a button
public class openuri22 extends Application {
   public static void main(String[] args) {
      Application.launch(args);
   }
   @Override
   public void start(Stage stgg) {
      String theURL = "https://www.tutorialspoint.com/index.htm";
      Button openURLBtnn = new Button("Open The URL");
      openURLBtnn.setMaxSize(100, 200);
      openURLBtnn.setOnAction(e -> getHostServices().showDocument(theURL));
      VBox vboxx = new VBox(openURLBtnn);
      Scene scene01 = new Scene(vboxx, 200, 200);
      scene01.setFill(Color.LIGHTGRAY);
      stgg.setScene(scene01);
      stgg.setTitle("Knowing the Host");
      stgg.show();
   }
}

Output

C:\java\javaprgstu>run.bat openuri22
C:\java\javaprgstu>javac --module-path "C:\Program Files\Java\javafx-sdk-19.0.2.1\lib" --add-modules javafx.controls,javafx.fxml openuri22.java
C:\java\javaprgstu>java --module-path "C:\Program Files\Java\javafx-sdk-19.0.2.1\lib" --add-modules javafx.controls,javafx.fxml openuri22

Conclusion

In this article, the different programs are given to open the given URL in the default browser. In the first approach the Desktop class methods are used and example are given. In first example the fixed URL is used in the code. In the second approach the Javafx libraries and the related methods are used for the same.

Updated on: 23-Mar-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements