Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launching a URL in a Java Swing application

Tags:

java

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?

like image 286
Dan Avatar asked Jan 22 '23 18:01

Dan


2 Answers

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.

like image 63
kdgregory Avatar answered Jan 25 '23 07:01

kdgregory


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:

  • Using the Desktop API in Java SE 6
  • How to Integrate with the Desktop Class
like image 20
coobird Avatar answered Jan 25 '23 07:01

coobird