在java中执行外部程序 [英] Execute external program in java
本文介绍了在java中执行外部程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试创建一个调用外部程序的应用程序,我必须传递两个参数。它没有给出任何错误。用c ++编写的program.exe拍照并修改了txt文件的内容。 java程序运行但它什么都不做
I tried to make an application that calls an external program that I have to pass two parameters. It doesn't give any errors.The program.exe,written in c++, takes a picture and modifies the content of txt file. The java program runs but it does nothing
这是我的示例代码
String[] params = new String [3];
params[0] = "C:\\Users\\user\\Desktop\\program.exe";
params[1] = "C:\\Users\\user\\Desktop\\images.jpg";
params[2] = "C:\\Users\\user\\Desktop\\images2.txt";
Runtime.getRuntime().exec(params);
推荐答案
从here
Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
更多信息这里
Other issues on how to pass commands here and here
这篇关于在java中执行外部程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文