全国统一服务热线:400-633-9193

Java 调用外部外部程序的方法

admin     未知     2017-02-11    1977

import java.io.*;
public class RunCommand {
    public RunCommand() {
     // 调用外部命令的时候一般不是单单开启那个程序,而是用要开启的程序做一些事 
        // 所以最好是像下面这样用数组来表示命令序列("命令","参数"[,"参数",...],"目标路径") 
        //String[] cmd = {"notepad.exe","C:\\settings.ini"};
        //String[] cmd = {"cmd.exe","/c","start","excel"};
        //String[] cmd = {"cmd.exe","/c","start","ping.exe","202.106.0.20"};
        String[] cmd = {"cmd.exe","/c","start","ipconfig.exe","/all"};
        try {
         // 下面一行是执行外部命令的关键 
            Process pro = Runtime.getRuntime().exec(cmd); 
         // 导致当前线程等待,如果必要,一直要等到由该 Process 对象表示的进程已经终止。 
            pro.waitFor();
         } catch (IOException e) {
            //deal with IOException
            e.printStackTrace();
         } catch (InterruptedException e) {
            //deal with InterruptedException
            e.printStackTrace();
         }
    }
    public static void main (String[] args) {
        new RunCommand();
    }

  分享到:  
0.2313s