一尘不染

用Java执行外部程序

java

我试图制作一个调用外部程序的应用程序,我必须传递两个参数。它没有任何错误。用c ++编写的program.exe可以拍照并修改txt文件的内容。Java程序可以运行,但是什么也没做

这是我的示例代码

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

阅读 439

收藏
2020-03-09

共1个答案

一尘不染

从这里可耻地借来的

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);
}
2020-03-09