java之如何实现调用启动一个可执行文件,exe

 1 /*

 2  * 运行可执行文件:.exe

 3  * 当要执行一个本地机器上的可执行文件时,

 4  * 可以使用java.lang包中的Runtime类,首先使用Runtime类,首先

 5  * 使用Runtime类声明一个对象

 6  *{

 7  *    Runtime sc =Runtime.getRuntime();

 8  *   sc可以调用exec(String command) 方法打开本地湖区上的可执行文件或执行一个操作。    

 9  * }

10  */

11 

12 

13 /*

14  * 不妨举列:

15  *    运用RUntime调用对象打开Windows平台上的记事本程序和浏览器。

16  */

17  package DEMO;  

18 

19 import java.io.File;

20 

21 public class test

22 {

23     public static void main(String args [])

24     {

25         try{

26             Runtime mt =Runtime.getRuntime();

27              //找到相对应的绝对路径。启动记事本文件

28             File  myfile =new File("c:\\Windows","Notepad.exe");

29             mt.exec(myfile.getAbsolutePath());

30              //创建新的文件路径,启动ie浏览器

31             myfile = new File("c:\\program Files\\Internet Explorer","IEXPLORE.www.sohu.com");

32             mt.exec(myfile.getAbsolutePath());   

33           }

34         catch(Exception e)

35         {

36           System.out.println(e);            

37         }

38     }

39 }

 

你可能感兴趣的:(java)