Tuesday, April 29, 2008

Java 6 and java.awt.Desktop

In a current application we're writing, we've got a use case where you can upload a file, and later download it by pressing the "View" button, which will open the file in correct application for the user.

We're using Java, and I thought this would be a nasty task full of pain and suffering, but to my suprise Java 6 includes a class which does exactly this: java.awt.Desktop. Here's some sample code which will open a PDF file on my disk.

import java.awt.*;
import java.io.*;

public class Test {
public static void main(String[] args) throws Exception {
Desktop.getDesktop().open(new File("/home/nathanr/Desktop/file.pdf"));

Cool, huh? Now I'm sure you Windows / .NET people are saying "that's not hard - we've been able to do that for $x years". Sure, but the above will work on Linux and Mac too.

No comments: