Advertisment

Launch Native Applications from Java

author-image
PCQ Bureau
New Update

JDesktop Integration Components, JDIC, is a Java desktop project at www.java.net that focuses on using native and 



platform-specific applications and facilities. According to the site -the JDesktop Integration Components (JDIC) project aims to make Java technology-based applications ('Java applications') first-class citizens of current desktop platforms without sacrificing platform independence. JDIC provides Java applications with access to facilities provided by the native desktop such as the mailer, the browser, and registered document viewing applications. Additionally it provides the mechanisms by which Java
applications can integrate into the native desktop such as registering Java applications as document viewers on the desktop and creating installer packages. 

Advertisment
Direct

Hit!
Applies

to:
Java developers
USP: Make your own Java application talk to native Windows application
Primary Link:

www.java.net
Google keywords: native integration java

In the past issues of PCQuest we have carried articles on embedding a Web browser (Internet Explorer) in a Java application and installing the Java application on the Windows system tray, using JDIC. In this article, we look at yet another feature of JDIC, which allows launching the native applications through Java. To understand how this is useful, consider the following example. Suppose you are writing a network browser application in Java and you browse through several files, and then you double click on a .doc file. Ideally, the Java network browser should launch MS Word and show the clicked .doc file. With JDIC, you can achieve this very easily. It also allows a Java application to edit and print a file, which will again launch the associated native applications.

But it does not stop here. You can launch the default e-mail client (say Outlook on a Windows desktop). What's more, you can pre-fill the To:, CC:, BCC:, Subject: and the body of the e-mail. And you can also add attachments. 

Advertisment

We go through short code snippets to understand how to code the above-mentioned things with JDIC. To compile and run the code below, you must install J2SDK version 1.4.2 or above which can be downloaded from java.sun.com. Also download the latest version of JDIC for Windows from https://jdic.dev.java.net. For this article we downloaded and used version 0.9.1 (filename: jdic-0.9.1-bin-windows.zip), which was the latest version as of this writing. We used J2SDK 1.5.0_01 (JDK 5 update 1) to compile and run the code. For convenience, put the bin directory of J2SDK installation in the PATH, so that 'javac ' and 'java' can be from anywhere. 

Create a directory named pcq and copy the files named jdic.jar and jdic.dll-which you will find in the zip archive-to this 



directory. 

Open an MS Word file



Suppose you have an MS Word file named file.doc in C:\. The Java code below will open this file in MS Word using

JDIC. 

Advertisment

import org.jdesktop.jdic.desktop.*;



import java.io.*;


public class JDICTest{


public static void main(String<> args){


File f = new File("c:\\file.doc");


try{


Desktop.open(f);


}


catch(DesktopException e){


System.out.println(e);


}}}








Save the above file as JDICTest.java. Change to the directory c:\pcq. Compile and run the code as:

javac -classpath jdic.jar JDICTest.java



java -cp jdic.jar;. JDICTest.java

Advertisment

Upon executing the second command, MS Word will launch and open up file.doc. The code itself is simple. Note the import statement for org.jdesktop.jdic.desktop.* at the top. The main statement which opens the file is Desktop.open( ). Here open( ) is a static method of the class Desktop which accepts a File object as the parameter. In our case the File object refers to the MS Word file named file.doc. Last but not the least, we must catch the DesktopException to handle any mishap while launching MS Word to open the file. 

Edit an HTML file



Next we see how to launch the default editor for a file through a Java application. Save the code below as JDICTest2.java:

import org.jdesktop.jdic.desktop.*;



import java.io.*;


public class JDICTest2{


public static void main(String<> args){


File f = new File("c:\\file.html");


try{


if(Desktop.isEditable(f)) Desktop.edit(f);


}


catch(DesktopException e){


System.out.println(e);


}}}








Advertisment

Compile and run it as before. This should open up the default editor configured for HTML files on your system, say FrontPage. The code remains almost the same expect for, we call the method Desktop.edit( ) instead of Desktop.open( ). Also note that we are checking whether the file is actually editable (not read only or protected) by using Desktop.isEditable(f) which returns false if the file is not editable. The code for printing a file is similar to the above, the

difference being, we use Desktop.print( ) and check whether the file is printable using Desktop.isPrintable( ). 

E-mail through Outlook



This facility is quite useful if you want to allow users to send e-mail through your desktop Java application, possibly a feedback e-mail, a purchase order or support e-mail. Note that this is

different from sending e-mail through the Java Mail API using, which you can dispatch, an e-mail to a SMTP server directly. This is more like the 'mailto:' feature in anchor () tag in HTML. 

Field Method
TO: msg.setToAddrs(java.util.ArrayList)
CC: msg.setCcAddrs(java.util.ArrayList)
BCC: msg.setBccAddrs(java.util.ArrayList)
Subject: msg.setSubject(String)
Body: msg.setBody(String)
Attachments: msg.setAttachments(java.util.ArrayList)
Advertisment

Getting down to the code, we first need to create an object of the class org.jdesktop.jdic.desktop.Message. Next, suppose you want to pre-fill the To: field, you call the method setToAddrs( ) of the Message class. The parameter to this method is an ArrayList object, which means you can specify multiple To: addresses where each

address is an item of the ArrayList. 



The table below lists downs the methods to pre-fill other fields in the mail client:

Here, msg is a Message object. Note that except for setSubject ( ) and setBody( ) all other methods accept an ArrayList as the

parameter. Another point to keep in mind is, while creating the ArrayList for setAttachments( ) methods, each item of the list must be a string specifying the absolute path to the file(s) which have to sent as attachments. Once the Message object is ready, you can launch Outlook using Desktop.mail( ) method with the

Message object as the parameter:Desktop.mail(msg).The code below will further clear out the concepts:

import org.jdesktop.jdic.desktop.*;



import java.util.*;


import java.io.*;

Advertisment

public class JDICTest3{



public static void main(String<> args){


Message msg = new Message();


ArrayList toList = new ArrayList();


toList.add("to@foo.com");


msg.setToAddrs(toList); 


ArrayList ccList = new ArrayList();


ccList.add("cc1@foo.com");


ccList.add("cc2@foo.com");


msg.setCcAddrs(ccList); 


ArrayList bccList = new ArrayList();


bccList.add("bcc@foo.com");


msg.setBccAddrs(bccList); 


msg.setSubject("Hi");


msg.setBody("Blah Blah Blah");


ArrayList attachmentList = new ArrayList();


attachmentList.add("c:\\file.doc");


attachmentList.add("c:\\file.html");


try{


msg.setAttachments(attachmentList);


}


catch(IOException e){


System.out.println(e);


}


try{


Desktop.mail(msg);


}


catch(DesktopException e){


System.out.println(e);


}}}



























Note that we must catch the IOException, while calling the method setAttachments( ).Save the above code as JDICTest3.java and, compile and run it as before. Upon execution, Outlook (or the default mail client on your Windows desktop) will launch with the fields pre-filled.

Shekhar Govindarajan, IT4Enterprise

Advertisment