tags:

views:

157

answers:

6

In Java 5, is there a default way to pass a URL to the system and have it launch the application associated with it? For example http:// links would usually open IE or Firefox, but things like itms:// should open iTunes.

If possible I would rather not use Runtime.exec() to start some external process directly, because this would be platform specific. If there is no other way, what would I call for Windows/OS X and Linux to cover the most popular ones?

+3  A: 

Use the Java Desktop API

 Desktop desktop =  Desktop.getDesktop();
 if (desktop.isSupported(Desktop.Action.BROWSE)) {
     desktop.browse(uri);
 }
ivan_ivanovich_ivanoff
I don't think that was added until Java 6.
erickson
Sorry, you're right.
ivan_ivanovich_ivanoff
A: 

Windows it's "start [URI]", OSX it's "open [URI]", Linux has no equivalent as it all depends upon their window manager.

As for whether itms:// opens iTunes, that all depends on their configuration.

Ivan's answer appears much better than mine.

caskey
A: 

Looks like you want java.awt.Desktop but I think it was introduced in 1.6 and wasn't available in 5...:-(

Alex Martelli
+1  A: 

I agree with ivan that Java Desktop API would work, but it's 6 only.

I know how to do it on Windows (it involves executing rundll32.dll), but I did some quick Googling and this link seems like your best shot.

Hope that helps.

MBCook
+1  A: 

For Java 5, try JDIC. It has cross-platform support for opening the OS's registered application for a file. If I remember correctly, this formed the basis for the similar API added in Java 6.

erickson
+1  A: 

BrowserLauncher does what you need.

Yishai