Registering a URL protocol handler in a multiple platforms

Dan picture Dan · Dec 22, 2009 · Viewed 8.3k times · Source

I an wanting to create a Java application that is installed on multiple platforms (Windows,Mac OS, Linux) as a part of this install I wish to register a URL protocol handler, so that my app loads when links are clicked.

i.e. I want something like this: myprotocol://example.com

Is there any sort of consolidated way of doing this? Or some sort of framework that extrapolates the difference across the different OS's.

Answer

Gary Rowe picture Gary Rowe · Dec 30, 2011

MultiBit implements this across a range of platforms

I've just been down this road for the MultiBit project (a lightweight Bitcoin client) where I had to implement launching and updating an app in response to a custom URI (in my case bitcoin:1sdfjsdfdkfdkjfdjfkjertn?amount=0.5&label=Some%20Text).

The way I had to implement it was to create a generic approach to receiving operating system events. A lot of this work was based on the Macify library and then rewritten to support multiple arbitrary platforms.

First some background. In general, protocol handlers are registered at the operating system side, rather than the browser side. This is because protocols are not confined to browsers and so a general support mechanism is required. Consequently, you need to create handlers for each platform you want to support.

For example, in the Mac world there is the EAWT library which is not available for distribution but provides access to the native event API. This means that your application needs to be able to locate this library at runtime and then reflectively work with the native classes (you can't hard code them since you can't guarantee that you will build your application on a platform that has the support library and you can't include it due to license restrictions). If that sounds like hard work - believe me it is.

On Windows you need to update the registry so that your application will be launched when someone uses that protocol. There is a useful set of instructions provided by Microsoft detailing this process.

On Linux, these commands generally do the trick for Gnome 2 (passing the URI in on the command line):

gconftool-2 -t string -s /desktop/gnome/url-handlers/bitcoin/command "bin/multibit %s"
gconftool-2 -s /desktop/gnome/url-handlers/bitcoin/needs_terminal false -t bool
gconftool-2 -t bool -s /desktop/gnome/url-handlers/bitcoin/enabled true

Edit July 2014

On Linux with Gnome 3 (Ubuntu 11.04+) the situation is a bit different relying on an exampleapp.desktop file placed in the /usr/share/applications folder followed by sudo update-desktop-database.

Enough talking - gimme the code!

You can find it in the MultiBit source code. I've not bothered to pull it out into it's own project but drilling down into the platform package and just pulling the code from there should be sufficient (it is self-contained). The application installs using IzPack and so the registry entries for Windows are also there to use.

The code was first introduced in the v0.3 branch, but will be mainstream from Q1 2012. It's all MIT license so you can do whatever you like with it. If you find bugs, please report them or, better, fix them and offer a pull request so others can benefit.