从java程序中调用其它程序

    技术2022-05-11  130

    java程序中调用其它程序

    http://developer.java.sun.com/developer/TechTips/1999/tt1214.html 讨论了RMI (Remote Method Invocation,远程方法调用)如何用于程序间通讯,另一个用于通讯的技术是Runtime.exec() 方法。你可以用这个方法从一个运行阿java程序中调用另一个程序。Runtime.exec 也允许你执行和程序相关的操作,例如控制程序的标准输入输出,等待程序的结束并得到它的退出状态。下面是一个简单的C程序,用来说明这些特性:

     

        #include <stdio.h>

     

        int main() {

            printf("testing/n");

            return 0;

        }

     

    这个程序写字符串"testing"到标准输出,然后用退出状态0终止程序。

    为了在java程序中执行这个简单的程序,先编译这个c程序:

     

        $ cc test.c -o test

    (译者注:对于linux用户,可以用gcc test.c –o test,对应windows用户可以用相应的c语言编译程序编译成可执行程序test.exe   

    (你的 C 编译器可能要求不同的参数)然后使用下面的代码调用那个程序:

     

        import java.io.*;

        import java.util.ArrayList;

       

        public class ExecDemo {

            static public String[] runCommand(String cmd)

                throws IOException {

       

                // set up list to capture command output lines

       

                ArrayList list = new ArrayList();

       

                // start command running

      

    Process proc = Runtime.getRuntime().exec(cmd);

    /**译者注:前面的声明应该改成java.lang.Process,即:

    java.lang.Process proc = Runtime.getRuntime().exec(cmd);

    如果不改的话可能编译不同通过,在译者的机器上使用jdk1.2,编译出现5个错误

    使用jdk1.4编译出现4个错误

                      */

                // get command's output stream and

                // put a buffered reader input stream on it

       

                InputStream istr = proc.getInputStream();

                BufferedReader br =

                    new BufferedReader(new InputStreamReader(istr));

       

                // read output lines from command

       

                String str;

                while ((str = br.readLine()) != null)

                    list.add(str);

       

                // wait for command to terminate

       

                try {

                    proc.waitFor();

                }

                catch (InterruptedException e) {

                    System.err.println("process was interrupted");

                }

       

                // check its exit value

       

                if (proc.exitValue() != 0)

                    System.err.println("exit value was non-zero");

       

                // close stream

       

                br.close();

       

                // return list of strings to caller

       

                return (String[])list.toArray(new String[0]);

            }

       

            public static void main(String args[]) throws IOException {

                try {

       

                    // run a command

       

                    String outlist[] = runCommand("test");

       

                    // display its output

       

                    for (int i = 0; i < outlist.length; i++)

                        System.out.println(outlist[i]);

                }

                catch (IOException e) {

                    System.err.println(e);

                }

            }

        }

     

    演示程序调用方法runCommand 实际运行程序。

     

         String outlist[] = runCommand("test");

                  

    这个方法使用一个输入流钩取程序的输出流,因此它可以读取程序的输出,然后将之存入一个字符串列表。

     

         InputStream istr = proc.getInputStream();

         BufferedReader br =

             new BufferedReader(new InputStreamReader(istr)); 

                   

         String str;

         while ((str = br.readLine()) != null)

             list.add(str);

       

    在所有的输出都被读取后,调用waitFor 等待程序终止,然后调用exitValue 读取程序的退出状态值。如果你做过很多系统编程的话,例如UNIX系统调用,这个方法你应该很熟悉。(这个例子假设当前路径包含在你的shell的执行路径上)

    如果你使用UNIX系统,你可以用:

        runCommand("ls -l");

    代替:

        runCommand("test");

    (译者注:一个更为通用的就是runCommand("java");,译者使用runCommand("dir");windows2000下得到的结果是程序IO异常,在linux系统下没有测试过。)

     

    得到当前路径下的所有文件的长列表。但是用这个办法得到列表突出了使用Runtime.exec 的一个基本的弱点――你调用的程序变成不可移植的。也就是说,Runtime.exec 是可移植的,在不同的java实现中都存在,但是被调用程序不一定是这样的,在Windows系统下没有程序叫"ls"

    假设你运行Windows NT 而你决定使用

     

        runCommand("dir");

     

    纠正这个问题,"dir" "ls" 的等同的命令。这不能工作,因为"dir" 不是一个可执行程序。它内建于shell (命令解释器) CMD.EXE。因此你应当使用:

     

        runCommand("cmd /c dir");

     

    这里 "cmd /c command" "调用shell 并执行指定的命令并退出。" 类似的,对于一个UNIX shell ,例如Korn shell,你可能应该用:

        runCommand("ksh -c alias");

     

    这里 "alias" shell 的内建命令。在这个情况下的输出是shell 的别名的列表。

    在上面获取目录列表的例子中,你可以用可移植的java程序到达相同的结果。例如:

     

        import java.io.File;

     

        public class DumpFiles {

            public static void main(String args[]) {

                String list[] = new File(".").list();

                for (int i = 0; i < list.length; i++)

                    System.out.println(list[i]);

            }

        }

     

    给出当前目录下所有文件和目录的列表。因此在大多情况下,使用ls/dir 可能没有意义。

    使使用Runtime.exec有意义的一个情况就是允许用户指定一个编辑器或者文字处理程序(就像Emacs 或者 Vi 或者Word) 编辑文件。这是大型程序的一个通常的特性。程序将有一个配置文件指定编辑器的本地路径,然后使用这个路径调用Runtime.exec

    Runtime.exec 的一个微妙的地方就是它如何查找文件。例如,你使用:

     

        Runtime.exec("ls");

     

    那么"ls" 如何被找到呢?在JDK 1.2.2 上的试验表明是搜索PATH 环境变量。这就像你在shell中执行命令时所发生的情况。但是文档中并没有说明这点,因此应该小心使用。你不能假设搜索路径已经设置。就像上面讨论的那样在有限的方式上使用Runtime.exec 可能更有意义,使用绝对路径。

    也有不同的Runtime.exec 允许你指定环境字符串数组。(译者注:具体方式请参看API文档)。


    最新回复(0)