Registering a URL protocol handler in a multiple platforms

9k views Asked by At

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.

6

There are 6 answers

1
OscarRyz On

As an alternative, using the JDIC project you can associate files with specific applications.

This may be useful for your proposes. But instead of registering the whole protocol ( which may be somehow complicated ) you may register the file type only.

So, a link like this:

 <a href="http://example.com/file.dan">Dan File</a>

May be opened with your application.

Here's the sample code to register your app to open that file type:

AssociationService serv = new AssociationService();
Association logassoc = new Association();

logassoc.addFileExtension("DAN"); 
logassoc.addAction( new Action("open", "C:\\WINDOWS\\JAVA.EXE -jar C:\\dan.jar %1"));

Here's the complete article: Understanding JDIC File-Type Associations

5
kdgregory On

I would recommend that you use Java Webstart rather than try to invent a new link scheme. It's already supported by any browser that has Sun Java installed.

3
OscarRyz On

In Firefox you can register your own protocol.

This article describes more about the protocol registration. Probably you could automate it from there.

0
Sam Barnum On

You'll probably need to do this in a platform-specific fashion. Here's how to do it in OS X

http://www.cocoadev.com/index.pl?HowToRegisterURLHandler

4
Gary On

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.

0
Xiaotao On

For Windows, you can modify registry in your installer,

REGEDIT4

[HKEY_CLASSES_ROOT\your_protocol]
@="URL: your_protocol"
"URL Protocol"="Your protocol name"

[HKEY_CLASSES_ROOT\your_protocol\DefaultIcon]
@="your_prog_location\your_prog.exe"

[HKEY_CLASSES_ROOT\your_protocol\shell]

[HKEY_CLASSES_ROOT\your_protocol\shell\open]

[HKEY_CLASSES_ROOT\your_protocol\shell\open\command]
@="your_prog_location\your_prog.exe %1"