Home >Java >javaTutorial >How to Open a URL in the Default Web Browser Using Java?
Opening a URL in the Default Web Browser
If you're seeking a method to navigate to a specific web page using your preferred browser from within a Java program, the solution lies with the java.awt.Desktop class.
Implementation:
To achieve this, follow these steps:
Import the necessary classes:
<code class="java">import java.awt.Desktop; import java.net.URI;</code>
Check for desktop support and browsing compatibility:
<code class="java">if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {</code>
Create a URI instance for the desired URL:
<code class="java">URI uri = new URI("http://www.example.com");</code>
Utilize the Desktop object to browse the URL:
<code class="java">Desktop.getDesktop().browse(uri);</code>
By implementing these steps, your Java program can effortlessly launch the default web browser and set the page to the specified URL.
The above is the detailed content of How to Open a URL in the Default Web Browser Using Java?. For more information, please follow other related articles on the PHP Chinese website!