在跨平台领域书写JAVA程序时,一个重要的问题就是无法直接访问操作系统的常用程序。比如用Java写好报表,总希望能自动用已注册的程序打开相应的文件(如.doc)。以前没有JDesktop时,可以用System.execute()方法直接启动特定的程序,但这种硬编码的方法相当不可靠。谁会知道客户机是是安装了MS WORD还是OpenOffice呢?JDK6中的JDesktop解决了所有问题。
如查你用打开系统的浏览器,或者记事本,或是邮件管理器等,JDesktop可以满足你的需求:

/**//*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication1;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;


/** *//**
*
* @author ljm-home
*/

public class JDesktopTest ...{
//定义Desktop
Desktop desktop=null;

public JDesktopTest()...{
//判断是否支持Desktop
if(Desktop.isDesktopSupported())
desktop=Desktop.getDesktop();
}
//用操作系统的记事本打开文件(Notepan、VI、GEdit等)

public void editTextFile(java.io.File file)...{
//如果支持Desktop,再判断是否支持edit

if(desktop!=null && desktop.isSupported(Desktop.Action.EDIT))...{

try ...{
desktop.edit(file);

} catch (IOException ex) ...{
Logger.getLogger(JDesktopTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//打操作系统的浏览器(IE,Firefox,Opera等)

public void lannchSystemBrowser(java.net.URI uri)...{
//如果支持Desktop,再判断是否支持launchBrowser

if(desktop!=null && desktop.isSupported(Desktop.Action.BROWSE))...{

try ...{
desktop.browse(uri);

} catch (IOException ex) ...{
Logger.getLogger(JDesktopTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//打操作系统的邮件管理器

public void sendEmail(java.net.URI uri)...{
//如果支持Desktop,再判断是否支持mail

if(desktop!=null && desktop.isSupported(Desktop.Action.MAIL))...{

try ...{
desktop.mail(uri);

} catch (IOException ex) ...{
Logger.getLogger(JDesktopTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//用操作系统的浏览器打开一个目录

public void openFile(java.io.File file)...{
//如果支持Desktop,再判断是否支持open

if(desktop!=null && desktop.isSupported(Desktop.Action.OPEN))...{

try ...{
desktop.open(file);

} catch (IOException ex) ...{
Logger.getLogger(JDesktopTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//用操作系统的默认打印机打印文件

public void printFile(java.io.File file)...{
//如果支持Desktop,再判断是否支持print

if(desktop!=null && desktop.isSupported(Desktop.Action.PRINT))...{

try ...{
desktop.print(file);

} catch (IOException ex) ...{
Logger.getLogger(JDesktopTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

/** *//**
* @param args the command line arguments
*/

public static void main(String[] args) ...{

try ...{
JDesktopTest jdt = new JDesktopTest();
//打开项目目录下的test.txt文件
jdt.editTextFile(new java.io.File("test.txt"));
Thread.sleep(500);
//打开http://www.youkuaiyun.com网页
jdt.lannchSystemBrowser(new java.net.URI("http://www.youkuaiyun.com"));
Thread.sleep(500);
//给Test@163.com发邮件
jdt.sendEmail(new java.net.URI("mailto:Test@163.com"));
Thread.sleep(500);
//打开D:TDDOWNLOAD网页
jdt.openFile(new java.io.File("d:/TDDOWNLOAD"));
Thread.sleep(500);
//打印test.txt
jdt.printFile(new java.io.File("test.txt"));

} catch (URISyntaxException ex) ...{
Logger.getLogger(JDesktopTest.class.getName()).log(Level.SEVERE, null, ex);

} catch (InterruptedException ex) ...{
Logger.getLogger(JDesktopTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
