The code below show you how to browse a website using the user’s default web browser. To get the default web browser you can use the Desktop
class browse(URI uri)
method call.
package org.kodejava.awt;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
public class RunningDefaultBrowser {
public static void main(String[] args) {
URI uri = URI.create("https://kodejava.org");
try {
// Get Desktop instance of the current browser context. An
// UnsupportedOperationException will be thrown if the
// platform doesn't support Desktop API.
Desktop desktop = Desktop.getDesktop();
// Browse the uri using user's default web browser.
desktop.browse(uri);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Latest posts by Wayan (see all)
- How do I use Proxy class to configure HTTP and SOCKS proxies in Java? - March 27, 2025
- How do I retrieve network interface information using NetworkInterface in Java? - March 26, 2025
- How do I work with InetAddress to resolve IP addresses in Java? - March 25, 2025