【Java】使用Runtime执行其他程序

 1 public class ExecDemo{
 2     public static void main(String[] args) {
 3         Runtime r = Runtime.getRuntime();
 4         Process p = null;
 5         try {
 6             p = r.exec("notepad");    
 7            //p=Runtime.getRuntime().exec("cmd /c start D://2.doc");            
 8            Thread.sleep(1000);    //程序暂停1秒钟
 9          p.destroy();
10         } catch (Exception e) {
11             System.out.println("Error executing notepad.");
12         }  
13     }
14 }

打开记事本,1秒钟之后关闭。

如果去掉第七行的注释,注释第六行,则为打开D盘下的2.doc文件,并且1秒后不关闭。

因为destroy的是执行的cmd命令,只是关闭了隐藏的cmd窗口。

 

posted on 2013-10-30 09:32  Allen Blue  阅读(236)  评论(0编辑  收藏  举报

导航