Keeping it Small and Simple

2007.05.12

Desktop integration in Java 6

Filed under: Java Programming — Lorenzo E. Danielsson @ 16:29

One of the new things in Mustang (Java 6) is that the Java platform now integrates better with the Desktop. That means, for instance, that Java can “figure out” the application associated with a particular file-type. It also means that you can write so-called panel applets in Java, but that may be the topic for another post. Here I will focus specifically only on getting Java to open your default application for a particular action or MIME type.

We are going to be using methods provided by java.awt.Desktop. You may want to read up on this class in your Java API documentation.

The first thing you need to do is find out if desktop integration is supported on the platform that the application is running on. So create the following simple program, called DesktopSupportPrinter.java:

 1 import java.awt.Desktop;
 2 
 3 import static java.awt.Desktop.isDesktopSupported;
 4 
 5 public class DesktopSupportPrinter {
 6     public static void main(String[] args) {
 7         if (isDesktopSupported())
 8             System.out.println("Desktop is supported.");
 9         else
10             System.out.println("Desktop is not supported.");
11     }
12 }

When you are done, compile and run the program. On my system (running Debian GNU/Linux I get the following output:

% java DesktopSupportPrinter
Desktop is supported.

Okay, so far so, good. Desktop integration is supported on my platform. If you instead get a message that desktop is not supported then the rest of this post won’t work for you. Sorry about that, go and bug Sun about it.

If you’ve already browsed through the API documentation for the class you will see that there are methods for checking whether specific aspects of the Desktop integration are supported. Let’s try that next:

 1 import java.awt.Desktop;
 2 import static java.awt.Desktop.isDesktopSupported;
 3 import static java.awt.Desktop.Action.BROWSE;
 4 import static java.awt.Desktop.Action.EDIT;
 5 import static java.awt.Desktop.Action.MAIL;
 6 import static java.awt.Desktop.Action.OPEN;
 7 import static java.awt.Desktop.Action.PRINT;
 8 
 9 public class DesktopSupportPrinter {
10     public static void main(String[] args) {
11         if (isDesktopSupported()) {
12             System.out.println("Desktop supported.");
13         } else {
14             System.out.println("There is no desktop support.");
15             System.exit(0);
16         }
17 
18         Desktop desktop = Desktop.getDesktop();
19 
20         if (desktop.isSupported(BROWSE))
21             System.out.println(" + Browser is supported.");
22         else
23             System.out.println(" - Browser is not supported.");
24 
25         if (desktop.isSupported(EDIT))
26             System.out.println(" + Editor is supported.");
27         else
28             System.out.println(" - Editor is not supported.");
29 
30         if (desktop.isSupported(MAIL))
31             System.out.println(" + Mail is supported.");
32         else
33             System.out.println(" - Mail is not supported.");
34 
35         if (desktop.isSupported(OPEN))
36             System.out.println(" + Open is supported.");
37         else
38             System.out.println(" - Open is not supported.");
39 
40         if (desktop.isSupported(PRINT))
41             System.out.println(" + Printing is supported.");
42         else
43             System.out.println(" - Printing is not supported.");
44     }
45 }
46 

When you are done, compile and run it to see what is supported on your platform. When I run it, I get the following results:

% java DesktopSupportPrinter
Desktop supported.
 + Browser is supported.
 - Editor is not supported.
 + Mail is supported.
 + Open is supported.
 - Printing is not supported.

I’m not sure what it means that my editor is not supported. I have Vim installed (and I have Gvim as well) and both my EDITOR and VISUAL environment variables are set, so I wonder..

But having Java tell you that a particular desktop action is supported is not enough. I’m not really sure about this, but I think it may be that Java only supports particular applications. Most actions fail by default on my system. Java cannot open my default (mailer) or my default browser (iceweasel). If the same happens to you, see below for a (partial) fix.

Well, back to the lesson. If you did as I told you earlier and opened up your API documentation, you will have noticed that the Desktop class contains a number of action methods: browse, edit(), mail(), open() and print(). What each one of these does should be pretty obvious. We can try one of the methods out. Let’s use the browse method to open the default web browser.

According to the API documentation, the browse method takes the URI of the location to open as an argument. So I wrote a program called BrowserLauncher, which opens up Google in your browser.

 1 import java.awt.Desktop;
 2 import java.io.IOException;
 3 import java.net.URI;
 4 
 5 import static java.awt.Desktop.isDesktopSupported;
 6 import static java.awt.Desktop.Action.BROWSE;
 7 
 8 public class BrowserLauncher {
 9     public static void main(String[] args) {
10         if (!isDesktopSupported()) {
11             System.err.println("No desktop support, aborting..");
12             System.exit(1);
13         }
14 
15         Desktop desktop = Desktop.getDesktop();
16 
17         if (!desktop.isSupported(BROWSE)) {
18             System.err.println("No browser support, aborting..");
19             System.exit(1);
20         }
21 
22         try {
23             desktop.browse(new URI("http://www.google.com"));
24         } catch (IOException e) {
25             e.printStackTrace();
26         }
27     }
28 }

When you are done, compile and try the program. If your browser is already running, it may be that the program opens up a new browser tab and opens Google’s search page in it. If things don’t work, then just keep reading.

Introducing Java to your preferred browser

As I mentioned above, the browse() method wasn’t working for me. I was just getting exceptions thrown. But I figured out how to fix it. Let’s look at what I did.

Check if there is a directory called .gconf in your home directory. Gconf is something that is very close to a Linux virus (installs itself even though you don’t want it). The directory holds a few files and directories. We need to edit specific files here. Here is the procedure I went through to get browsing to work on my system.

  1. Edit the files ~/.gconf/desktop/gnome/applications/browser/%gconf.xml and ~/.gconf/desktop/gnome/url-handlers/http/%gconf.xml. In each file replace any mention of epiphany with iceweasel (or whatever browser you use). Not sure if it is necessary to edit both files, but just to be on the safe side..
  2. Check for running instances of gconfd with ps ax | grep gconf. Kill them, if they exist
  3. Restart gconfd, with /usr/lib/libgconf2-4/gconfd-2 &

After doing this, the browse() method started working. I’m assuming that you apply a similar principle to get the mailer working as well, but as of yet, I haven’t found the file that contains that information yet. If you know where it is, please let me know. A comment will do fine.

I know this is a bit kludgy, but desktop support in Java is new, and I’m guessing that the developers at Sun were in a hurry to get it out the door. I’m hoping that this gets simplified in Java 7. It would be so simple to just let Java parse a text file, maybe called ~/.preffered-applications instead of some obscure XML files managed by an application that exists only on my system because it was a dependency of another package that I did choose to install.

Advertisements

3 Comments »

  1. a good class in jdk 1.6, but it simplify java too much

    Comment by ITCRAPS — 2007.12.08 @ 08:37

  2. @ITCRAPS: Take a look at the name of the blog again. There are already thousands of blogs out there that do advanced stuff. I try to keep things as simple as possible here.

    Comment by Lorenzo E. Danielsson — 2007.12.08 @ 10:03

  3. […] Desktop Integration in Java 6  Cetak Artikel Ini […]

    Pingback by Java - Vavai » Tips & Trick » Tips Java : Menjalankan File dengan Aplikasi Default — 2008.01.29 @ 09:14


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: