Open In App

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

Last Updated : 19 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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)

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads