Dan
Dan

Reputation: 29365

Launching a URL in a Java Swing application

How do I launch a URL in the user's default browser, in code from a Java Swing application?

There is this Netbeans library, but the jar dont seem to contain the classes mentioned in the example.

And there seems to be a number of old bespoke examples around.

But are there any killer solutions?

Upvotes: 4

Views: 2835

Answers (3)

kdgregory
kdgregory

Reputation: 39606

If you're running on JDK 1.6, you java.awt.Desktop.

Desktop.getDesktop().browse(new java.net.URI("www.google.com"));

If running on an earlier JDK, I believe that you can download the JDIC library. Or hack something together by spawning processes.

Upvotes: 6

coobird
coobird

Reputation: 160954

To expand upon kdgregory's answer, the The Java Desktop API, available from Java 6, provides integration with the desktop with functionality such as launching default web browsers and mail clients.

Launching a web browser can be achieved by using the Desktop.browse method.

For example, launching http://stackoverflow.com can be acheived by the following:

Desktop.getDesktop().browse(new URI("http://stackoverflow.com"));

More information:

Upvotes: 2

Yishai
Yishai

Reputation: 91871

You can look at BrowserLauncher, although the latest version of the JDK are trying to make that obsolete.

Upvotes: 1

Related Questions