服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

从java程序中调用其它程序


  翻译:cherami email:cherami@163.net

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");
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);
}
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);
}
}

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

扫描关注微信公众号