Home >Java >javaTutorial >How to Open Any URL in the Default Web Browser Using Java?
In this article, we will explore how to open a specified URL in the user's default web browser using Java.
To achieve this, Java provides the java.awt.Desktop class, a platform-independent interface for accessing the desktop environment. Here's a code snippet that demonstrates its usage:
import java.awt.Desktop; import java.net.URI; // ... if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { Desktop.getDesktop().browse(new URI("http://www.example.com")); }
This code accomplishes the following:
To use this code in your program, simply replace the placeholder URL "http://www.example.com" with the actual URL you want to open. Then, call the provided code snippet where necessary. For instance, you could call it from a button click event handler to open the specified URL when a button is clicked.
The above is the detailed content of How to Open Any URL in the Default Web Browser Using Java?. For more information, please follow other related articles on the PHP Chinese website!