1. 程式人生 > >java呼叫第三方應用程式

java呼叫第三方應用程式

1. java呼叫第三方應用使用 ProcessBuilder 物件來呼叫   

例子位置:

  xc-service-manage-media-processor/test/TestProcessBuilder

 

//建立processBuilder物件
        ProcessBuilder processBuilder = new ProcessBuilder();
        //設定第三方應用程式的命令
//        processBuilder.command("ping","127.0.0.1");
        processBuilder.command("ipconfig");

        //將標準輸入流和錯誤流合併
        processBuilder.redirectErrorStream(true);
        //啟動一個程序(相當於在命令列中輸入命令)
        Process process = processBuilder.start();

        //通過標準輸入流來拿到正常和錯誤的資訊
        InputStream inputStream = process.getInputStream();

        //轉成字元流
        InputStreamReader reader = new InputStreamReader(inputStream,"gbk");
        //緩衝
        char[] chars = new char[1024];
        int len = -1;
        while ((len = reader.read(chars))!=-1){
            String string = new String(chars,0,len);
            System.out.println(string);
        }
        inputStream.close();
        reader.close();