今天小试了几个例子
参考资料:老紫竹的博客
http://blog.csdn.net/java2000_net、JDK6API
- package desktop;
- import java.awt.Desktop;
- import java.io.File;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.net.URL;
- /***
- * JDK6:打开系统关联程序;启动默认浏览器打开网页
- * **/
- public class DesktopDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- DesktopDemo demo=new DesktopDemo();
- // demo.openFile();
- demo.operBrowse();
- }
- /***
- * 使用默认系统关联的程序打开文件
- * **/
- private void openFile(){
- Desktop desktop = Desktop.getDesktop();
- File file = new File("d://temp.xls");
- if (!file.exists())
- System.out.println("file not exist");
- else
- try {
- desktop.open(file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /***
- * 启动默认浏览器打开网页
- * **/
- private void operBrowse(){
- Desktop desktop=Desktop.getDesktop();
- URI uri;
- try {
- uri = new URI("http://www.google.cn");
- desktop.browse(uri);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }