Friday 4 November 2016

Launch URI Scheme over java file

URI stands for Uniform Resource Identifier, used to uniquely identify a resource.

What is URI Scheme?
URI scheme tells how to interpret given URI.

In this post, I am going to explain how to launch URI scheme in windows. I am going to develop simple java application, where it read an url and open it in browser.

Following is my URI open class.

import java.awt.Desktop;
import java.io.IOException;

public class URIOpen {
 public static void main(String args[]) {
  if (args.length == 0) {
   return;
  }

  String uri = args[0];

  try {
   java.net.URI theURI = java.net.URI.create(uri);
   
   // System.out.println(theURI.getScheme()); => myDocs
   String uriBrowsablePart = theURI.getRawSchemeSpecificPart();
   
   // System.out.println(uriBrowsablePart); => http://google.com
   Desktop.getDesktop().browse(java.net.URI.create(uriBrowsablePart));
   
   // the above statement will open default browser on http://google.com
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
  
 }
}

Compile the class and generate URIOpen.class file.

It is time to register our application with windows registry.

Step 1:
To register an application to handle a particular URI scheme, add a new key, along with the appropriate sub keys and values, to HKEY_CLASSES_ROOT. The root key must match the URI scheme that is being added. For instance, to add an ‘myDocs:’ scheme, add an alert key to HKEY_CLASSES_ROOT, as follows.

Open run prompt and type ‘regedit’ and press Enter. It opens registry window like below.


Right click on ‘HKEY_CLASSES_ROOT’ and add new key to it.

Give the key name as ‘myDocs’.

You need to add new value under the key ‘myDocs’. Under this new key, the URL Protocol string value indicates that this key declares a custom pluggable protocol handler. Without this key, the handler application will not launch. The value should be an empty string.

Right click on ‘myDocs’ -> New -> String Value. Give the string value as ' URL Protocol’.

You need to add the value of ‘Default’ to "URL : myDocs Protocol"

Now you need to add two new keys ‘DefaultIcon and shell’ to ‘myDocs’.

Right click on myDocs -> New -> Key. Give the key value as ‘DefaultIcon’.

Right click on myDocs -> New -> Key. Give the key value as ‘shell’.


The name of the first key under the shell key should be an action verb, such as open. Under this key, a command key or a DDEEXEC key indicate how the handler should be invoked. The values under the command and DDEEXEC keys describe how to launch the application handling the new protocol.

Right click on shell -> New -> key. Give the key name as ‘open’.

Give new key named ‘command’ under the key ‘open’.

Right click on open -> New -> key. Give the key name as ‘command’.

Following is the complete tree structure.



Update the (Default) value field of the command key like below.

"C:\Program Files (x86)\Java\jdk1.8.0_102\bin\java.exe"  "C:\Users\I335077\Documents\Study\Miscellaneous\examples\URIOpen" "%1"

When user clicks on ‘myDocs:someURL’, it launch the application URIOpen.

For example, Open run prompt and type ‘mydocs:http://google.com’, it opens google.com in browser.

No comments:

Post a Comment