【博客搬家】如何取得由Java程式呼叫的外在程式的執行結果?

作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 12
時間: Mon Jun 17 22:46:54 2002

12.如何取得由Java程式呼叫的外在程式的執行結果?

每個application都會有一個Runtime類別的instance,讓你可以和執行時期的環境做互動。
你可以呼叫 getRuntime()來取得這個instance。

Runtime.exec()的參數指的是要執行的程式。Runtime.exec()會產生一個原生的process並
傳回一個 Process的子類別,可以用來控制或者取得這個原生process的資訊。這個process
既不使終端機介面,也不用命令列介面,而是將他的I/O操作都轉向到這三個串流:

Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream().

下面是一個例子:

12345678910111213141516171819202122232425
import java.io.*;
public class ReadingProcess {
    public static void main(String args[]){
        //Provide a command as an input argument
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(args[0]);
        BufferedWriter buffOut = new BufferedWriter(
                                 new OutputStreamWriter(
                                        process.getOutputStream());
        BufferedReader buffIn = new BufferedReader(
                                new InputStreamReader(
                                        process.getInputStream());

        buffOut.write("//Some String");
        buff.flush(); //Ensure that the output reaches the process

        String s;

        if((s=buffIn.readLine())!= null)
        System.out.println(s);

        buffOut.close();
        buffIn.close();
    }
}




注意!
根據不同的底層作業平台,你可能會遇到一些停滯的狀況。有一些平台的I/O串流只有很小
的buffer,如果不能快速的對標準I/O串流做讀寫,則可能會發生停滯的狀況。

12. How do you retrieve output from external programs that have been executed by
a program developed on the Java Platform?

Every application written on has one instance of the class Runtime to allow
interaction with its runtime environment. You retrieve this instance by calling
getRuntime().

The arguments of Runtime.exec() are commands to be executed in the runtime
environment. The Runtime.exec() methods create native processes and return a
subclass of Process which provides methods to control and retrieve information
from the underlying process. The process has neither a terminal nor a console
thus all its standard I/O operations are redirected to the parent process via
three streams: Process.getOutputStream(), Process.getInputStream(),
Process.getErrorStream().

Note the following sample:

12345678910111213141516171819202122232425
import java.io.*;
public class ReadingProcess {
    public static void main(String args[]){
        //Provide a command as an input argument
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(args[0]);
        BufferedWriter buffOut = new BufferedWriter(
                                 new OutputStreamWriter(
                                        process.getOutputStream());
        BufferedReader buffIn = new BufferedReader(
                                new InputStreamReader(
                                        process.getInputStream());

        buffOut.write("//Some String");
        buff.flush(); //Ensure that the output reaches the process

        String s;

        if((s=buffIn.readLine())!= null)
        System.out.println(s);

        buffOut.close();
        buffIn.close();
    }
}




A note of warning - you may experience some hanging depending on the type of
native platform you are running on. Some native platforms have small buffers for
standard I/O streams, failure to quickly write/read from the standard
output/input streams into those of the subprocess may cause the subprocess to
block.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值