在该项目中需要运行本地文件或应用程序,JDK6添加后Desktop类别。可以直接使用。这使得有可能在程序中无论什么应用程序可以打开的。例:打开pdf文件,当地福昕是默认打开。执行程序将使用福昕开放pdf文件。
package test; import java.awt.Desktop; import java.io.File; public class DesktopTest { public static void main(String[] args) { //平台是否支持Desktop类 if(Desktop.isDesktopSupported()) { //实例化 Desktop desk= Desktop.getDesktop(); try { //创建一个java文件系统 File file=new File("D://帮助文档//jQuery-api-1.7.1_20120209.chm"); //调用open(File f)方法打开文件 desk.open(file); }catch(Exception e) { System.out.println(e.toString()); } } } }
getDesktop
public static Desktop getDesktop()
- 返回当前浏览器上下文的
Desktop
实例。一些平台不支持 Desktop API。能够使用isDesktopSupported()
方法来确定是否支持当前桌面。 -
- 返回:
- 当前浏览器上下文的 Desktop 实例
- 抛出:
HeadlessException
版权声明:本文博客原创文章,博客,未经同意,不得转载。