Saturday, October 25, 2025
HomeLanguagesJavaJava Program to Open Input URL in System Default Browser in Windows

Java Program to Open Input URL in System Default Browser in Windows

URL is the URL you want to display in the default Web browser. For example, wire http://www.site.com/report.html to this input to display the HTML file report.html on the Web Server www.site.com in the browser.

Our problem is to write a java program to open Input URL in System Default Browser in Windows.

To open any URL we use different predefine files of java to operate with our Desktop.

  1. java.awt.Desktop: We use java.awt.Desktop class because java.awt.Desktop allow us to interact with different capability of our Desktop. For Example Launching User default Browser, Launching user default mail Client etc.
  2. java.net.URI: java.net.URI is used to Represent User Resource Identifier reference.

Algorithm:

  • Firstly, we create an Object of our-defined class that we import by java.awt.Desktop.
  • During the creation of an object, we use getDesktop() method that returns the Desktop instance of the current desktop context. On some platforms the Desktop API may not be supported, in that case we use isDesktopSupported() method to determine if the current desktop is supported.
  Desktop desk=Desktop.getDesktop();
  • Then we use browse() method where we enter our URL that we want to open on our desktop by new URI().
  desk.browse(new URI("http://xyz.com"));

Java




// Java Program to Open Input URL in 
// System Default Browser in Windows
  
import java.awt.Desktop;
import java.io.*;
import java.net.URI;
  
class GFG {
    public static void main(String[] args)
             throws Exception
    {
        Desktop desk = Desktop.getDesktop();
        
        // now we enter our URL that we want to open in our
        // default browser
        desk.browse(new URI("http://xyz.com"));
    }
}


Output

(Our URL "http://xyz.com" will open in our desktop default browser)
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS