找回密码
 加入
搜索

[代码] 从java程序中调用其它程序

[复制链接]
发表于 2006-10-30 21:09:46 | 显示全部楼层 |阅读模式
http://developer.java.sun.com/developer/TechTips/1999/tt1214.html 讨论了RMI (Remote Method Invocation,远程方法调用)如何用于程序间通讯,另一个用于通讯的技术是Runtime.exec() 方法。你可以用这个方法从一个运行阿java程序中调用另一个程序。Runtime.exec 也允许你执行和程序相关的操作,例如控制程序的标准输入输出,等待程序的结束并得到它的退出状态。下面是一个简单的C程序,用来说明这些特性:



  1. #include



  2. int main() {

  3. printf("testing\n");

  4. return 0;

  5. }
复制代码



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

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



$ cc test.c -o test

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

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



  1. import java.io.*;

  2. import java.util.ArrayList;



  3. public class ExecDemo {

  4. static public String[] runCommand(String cmd)

  5. throws IOException {



  6. // set up list to capture command output lines



  7. ArrayList list = new ArrayList();



  8. // start command running



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

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

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

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

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

  14. */

  15. // get command's output stream and

  16. // put a buffered reader input stream on it



  17. InputStream istr = proc.getInputStream();

  18. BufferedReader br =

  19. new BufferedReader(new InputStreamReader(istr));



  20. // read output lines from command



  21. String str;

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

  23. list.add(str);



  24. // wait for command to terminate



  25. try {

  26. proc.waitFor();

  27. }

  28. catch (InterruptedException e) {

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

  30. }



  31. // check its exit value



  32. if (proc.exitValue() != 0)

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



  34. // close stream



  35. br.close();



  36. // return list of strings to caller



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

  38. }



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

  40. try {



  41. // run a command



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



  43. // display its output



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

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

  46. }

  47. catch (IOException e) {

  48. System.err.println(e);

  49. }

  50. }

  51. }
复制代码


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



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



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



  1. InputStream istr = proc.getInputStream();

  2. BufferedReader br =

  3. new BufferedReader(new InputStreamReader(istr));



  4. String str;

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

  6. 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程序到达相同的结果。例如:


  1. import java.io.File;



  2. public class DumpFiles {

  3. public static void main(String args[]) {

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

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

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

  7. }

  8. }
复制代码


给出当前目录下所有文件和目录的列表。因此在大多情况下,使用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文档)。

Archiver|手机版|小黑屋|乱弹琴|重庆网址之家|多来米发 |

GMT+8, 2026-10-13 08:56 PM

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表