In the Swing based java applications I write, the requirement usually exists to allow the app to open up other files such as Word Documents, PDF's, HTML pages, etc. Since I try not to limit myself to just the file types defined in requirements gathering, I looked for a more general solution that will allow my apps to open any file type. After all, if on my Windows or Linux desktops I can double click on a file name to open the appropriate program, then why can't my java applications?
My first take at this was to just use the java runtime, and assume my code would only run on windows 2000 or greater. The code at that point to open a file looked like this:
String cmd = "cmd /c \"" + fileName + "\"";
log.debug("Opening file with command: "+cmd);
java.lang.Runtime.getRuntime().exec(cmd);
Well, after that was in place for a few months, we wanted allow our application to also run on Linux. Uh oh, now the code above no longer works...
I searched around for a few minutes, and found a wonderful library called the JDesktop Integration Components (JDIC).
I downloaded the JDIC build, and included the jdic.jar in my project. Now my code to open files morphed into the following:
org.jdesktop.jdic.desktop.Desktop.open(new File(fileName));
So much simpler! The JDIC will launch the given file using the associated application as defined in the OS. The javadoc for the Open command looks like:
open
public static void open(java.io.File file)
throws DesktopException
Launches the associated application to open the given file.
Parameters:
file - the given file.
Throws:
DesktopException - if the given file is not valid,
or there is no associated application, or the
associated application fails to be launched.
I now have a simple utility class that I include in the framework package for my applications that allows me to open files, and get the file extension of a given file. Believe it or not, the most obscure file types I ended up using this code to open were for an EMR/medical application I was writing. Within that application I had to open various videos from ultrasounds, digitized X-Rays, etc. As long as the appropriate program to view those files was on the client PC, the code I include in my application ran could open it's associated file type.
The code for this utility class is listed below. Please feel free to use it in your applications.
/**
* Copyright (c) 2002-2004 by Timothy E. Archer. All rights reserved.
*
* $Id: FileUtil.java,v 1.1 2004/07/07 02:55:03 tima Exp $
*
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL TIMOTHY E. ARCHER BE LIABLE FOR ANY
* DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
package com.tima.util;
import java.io.*;
import java.util.*;
import org.apache.log4j.*;
/**
* General File manipulation/maintenance routines.
* This class provides helper functions
* to do things like open files, etc.
*
* @author Tim Archer 10/14/03
* @version $Revision: 1.1 $
*/
public class FileUtil {
static Logger log = Logger.getLogger(FileUtil.class);
/**
* Open up the file using the default OS's method for
* handling the file open.
* This method uses the Java Desktop Integration
* components (JDIC) to open the file with the method
* org.jdesktop.jdic.desktop.Desktop.open(new File(fileName));
*
* @param fileName The name of the file to open.
* @throws Exception If an error occurs.
*/
public static void openFile(String fileName) throws Exception {
/* Old logic,
* This method will only work on the MS windows platform
* (win2k and greater), and opens the file through the command:
* cmd /c "fileName"
String cmd = "cmd /c \"" + fileName + "\"";
log.debug("Opening file with command: "+cmd);
java.lang.Runtime.getRuntime().exec(cmd);
*/
//TEA 12/19/04, New method, uses JDIC
org.jdesktop.jdic.desktop.Desktop.open(new File(fileName));
}
/**
* Open up the file using the default OS's method
* for handling the file open.
* This method uses the Java Desktop Integration
* components (JDIC) to open the file with the method
* org.jdesktop.jdic.desktop.Desktop.open(new File(fileName));
*
* @param file The reference to the file to open.
* @throws Exception If an error occurs.
*/
public static void openFile(File file) throws Exception {
openFile(file.getAbsolutePath());
}
/**
* Return the extension of the file with the specified filename,
* such as pdf, doc, etc.
* This method will basically just extract the last three
* characters off the end of the file name.
*
* @param fileName The name of the file to get the extension for.
* @return String The extension of the file, or empty string if no
* extension could be determined.
* @throws Exception If an error occurs.
*
*/
public static String getFileExtension (String fileName)
throws Exception {
String fileExtension = "";
//
//Determine the file extension
//
if (fileName != null && fileName.length() > 3) {
fileExtension = fileName.substring(fileName.length()-3);
}
return fileExtension;
}
/**
* Return the extension of the specified file,
* such as pdf, doc, etc.
* This method will basically just extract the last
* three characters off the end of the file name.
*
* @param file The reference to the file to get the extension for.
* @return String The extension of the file, or empty string
* if no extension could be determined.
* @throws Exception If an error occurs.
*
*/
public static String getFileExtension (File file) throws Exception {
String fileExtension = "";
//
//Determine the file extension
//
if (file != null) {
fileExtension = getFileExtension(file.getAbsolutePath());
}
return fileExtension;
}
/**
* Main unit test.
*
* @param args Command line arguments.
*/
public static void main (String args[]) {
try {
openFile("c:\\temp\\t.txt");
} catch (Exception exc) {
log.error("An error occurred. "+exc.toString(), exc);
}
log.debug("main finished.");
}
}

del.icio.us
Digg
StumbleUpon