Registering a URL protocol handler in a multiple platforms

断了今生、忘了曾经 提交于 2021-02-04 09:22:06

问题


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.


回答1:


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.




回答2:


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"



回答3:


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




回答4:


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:


In Firefox you can register your own protocol.

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




回答6:


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



来源:https://stackoverflow.com/questions/1947209/registering-a-url-protocol-handler-in-a-multiple-platforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!