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,458
  • 5
  • 41
  • 71
  • 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
  • 18,603
  • 25
  • 79
  • 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,130
  • 7
  • 36
  • 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
  • 257,800
  • 54
  • 385
  • 479
1

BrowserLauncher does what you need.

Yishai
  • 88,049
  • 30
  • 183
  • 256
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
  • 11,695
  • 2
  • 25
  • 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
  • 811,175
  • 162
  • 1,198
  • 1,373