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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
To open any URL we use different predefine files of java to operate with our Desktop.
- 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.
- java.net.URI: java.net.URI is used to Represent User Resource Identifier reference.
- 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.
- Then we use browse() method where we enter our URL that we want to open on our desktop by new URI().
(Our URL "http://xyz.com" will open in our desktop default browser)