1

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?

Daniel Schneller
  • 13,728
  • 5
  • 43
  • 72
  • See also: http://stackoverflow.com/questions/526037/java-how-to-open-user-system-preffered-editor-for-given-file – erickson Jun 18 '09 at 23:58

6 Answers6

4

Use the Java Desktop API

 Desktop desktop =  Desktop.getDesktop();
 if (desktop.isSupported(Desktop.Action.BROWSE)) {
     desktop.browse(uri);
 }
ivan_ivanovich_ivanoff
  • 19,113
  • 27
  • 81
  • 100
2

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
  • 14,424
  • 7
  • 37
  • 41
1

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
  • 265,237
  • 58
  • 395
  • 493
1

BrowserLauncher does what you need.

Yishai
  • 90,445
  • 31
  • 189
  • 263
0

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
  • 12,305
  • 2
  • 26
  • 27
0

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

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395