23

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.

4
  • And you're expecting to have ... what? something like <a href=youprotocol://server.com>Launch my app</a>` ??? Commented Dec 22, 2009 at 15:51
  • 2
    Yeah? Like say spotify does. That an issue? Commented Dec 22, 2009 at 16:05
  • Hey! I'm currently investigating the same problem. Did you find a good cross-platform solution? Commented Mar 2, 2011 at 15:17
  • I've added an answer that provides access to the necessary source code to get this working for an arbitrary protocol across Mac, Windows and Linux. Commented Jan 8, 2012 at 23:23

6 Answers 6

17

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't see any code relating to Mac, even though you mentioned a bit about how difficult it was to get going with EAWT. Where is that?
Look for the MacApplication class
What about the Windows registry modifications? Are they executed from Java code?
No you'll have to use a custom installer, such as Install4j, to do this
4

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"

Comments

2

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

1 Comment

Using a file download instead of a protocol will cause other troubles. For security reason not all browsers will allow a default action to be set, instead they will always prompt the user to open or save the file. The file will also be saved to a download folder, which may require cleaning up. Using URI scheme the file is saved to a temp folder automatically, and compared to a file download, more browsers will allow you to set a default action, which means user only get a prompt once.
0

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.

5 Comments

I think Webstart wont cut it as the application will need to do things outside of Webstart security restrictions, I think installation is necessary.
By signing your application you can get full access.
As long as your user says "OK", a Webstart app is indistinguishable from any other.
Webstart wont do the trick as if Java is not installed the jnlp file is not recognised, which confuses laymen users. Plus I need the file\utl association.
The JNLP URI scheme was added in some build of 1.7, so there might be some users with Java installed but without the URI scheme.
0

In Firefox you can register your own protocol.

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

3 Comments

Thanks, but a ff extension would not be comprehensive enough.
@Thorbjørn Not being reliant on Firefox being installed, need to assume the user could be using any of the main browsers - needs to be an installed application (I think).
@Dan: That's what the second article is all about. If you manage to automate that registration process then you're done. There's an alternative though, based not in protocol name, but in the file extension. I'll post the link in another answer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.