java 在java代码中执行一个带参数的外部程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2921621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing in java code an external program that takes arguments
提问by questioner
Process p;
String line;
String path;
String[] params = new String [3];
params[0] = "D:\prog.exe";
params[1] = picA+".jpg";
params[2] = picB+".jpg";
try
{
p = Runtime.getRuntime().exec(params);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
}
catch (IOException e)
{
System.out.println(" procccess not read"+e);
}
I don't get any error, just nothing. In cmd.exe prog.exe is working fine.
我没有收到任何错误,只是什么都没有。在 cmd.exe prog.exe 工作正常。
What to improve in order to make this code working?
为了使此代码正常工作,需要改进什么?
采纳答案by Maurice Perry
Perhaps you should use waitFor() to obtain the result code. This means that the dump of the standard output must be done in another thread:
也许您应该使用 waitFor() 来获取结果代码。这意味着标准输出的转储必须在另一个线程中完成:
String path;
String[] params = new String [3];
params[0] = "D:\prog.exe";
params[1] = picA+".jpg";
params[2] = picB+".jpg";
try {
final Process p = Runtime.getRuntime().exec(params);
Thread thread = new Thread() {
public void run() {
String line;
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
} catch (IOException e) {System.out.println(" procccess not read"+e);}
};
thread.start();
int result = p.waitFor();
thread.join();
if (result != 0) {
System.out.println("Process failed with status: " + result);
}
回答by Robert
Use a p = new ProcessBuilder(params).start();instead of
使用p = new ProcessBuilder(params).start();代替
p = Runtime.getRuntime().exec(params);
p = Runtime.getRuntime().exec(params);
Other than that looks fine.
除此之外看起来还不错。
回答by Andrei Fierbinteanu
I just tried this on my system:
我刚刚在我的系统上试过这个:
public static void main(String[] args) throws IOException {
String[] params = { "svn", "help" };
Process p = Runtime.getRuntime().exec(params);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
and it worked fine. Are you sure the program you're using actually prints something to the console? I see it takes jpegs as input, maybe it writes to a file, not stdout.
它工作正常。你确定你正在使用的程序真的会在控制台上打印一些东西吗?我看到它需要 jpegs 作为输入,也许它写入文件,而不是标准输出。
回答by paragjain
Just like reading from the input stream of the process, you can also read from the error stream like this:
就像从进程的输入流中读取一样,您也可以像这样从错误流中读取:
Process p;
String line;
String path;
String[] params = new String [3];
params[0] = "D:\prog.exe";
params[1] = picA+".jpg";
params[2] = picB+".jpg";
try {
p = Runtime.getRuntime().exec(params);
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
BufferedReader error =
new BufferedReader
(new InputStreamReader(p.getErrorStream()));
while ((line = input.readLine()) != null)
System.out.println(line);
while ((line = error.readLine()) != null)
System.out.println(line);
input.close();
error.close();
} catch (IOException e) {
System.out.println(" procccess not read"+e);
}

