【发布时间】:2013-08-25 07:25:23
【问题描述】:
编写一些 Java 代码以在 Linux 下运行文本可执行文件,我无法打印出它的输出。这个可执行文件实际上是一个nmap -sP,因此接收到参数。
每次调用编译后的类,我只能看到第一行输出,其他什么都看不到。
这是 runFile.java 文件:
import java.lang.Runtime;
import java.lang.Process;
import java.io.*;
import java.lang.InterruptedException;
public class runFile {
public static void main (String args[]) throws IOException, InterruptedException {
Runtime r = Runtime.getRuntime();
Process p = r.exec("/home/diegoaguilar/Dropbox/Buap/SO/file.exe "+args[0]+args[1]);
InputStream stream = p.getInputStream();
BufferedReader reader = new BufferedReader (new InputStreamReader(stream));
String salida = reader.readLine();
while (salida != null) {
System.out.println(salida);
salida = reader.readLine();
}
//p.waitFor();
}
}
所以,这就是file.exe的内容:
nmap -sP $segment1-$segment1
无论我调用 runFile 使用什么参数,无论是否有效,它总是会打印到控制台,类似于第一行:
在 2013-08-25 02:09 CDT 开始 Nmap 5.21 (http://nmap.org)
【问题讨论】:
-
你需要读取进程的InoutStream(这是进程的输出),for example和exmple。一个将进程的输出直接通过管道输出到标准输出,另一个使用线程读取 InputStream 以免阻塞当前线程
标签: java executable runtime.exec