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 a .txt
file.
The Java program runs but it does nothing-
Here is my sample code:
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);
This question is related to
java
runtime
runtime.exec
import java.io.*;
public class Code {
public static void main(String[] args) throws Exception {
ProcessBuilder builder = new ProcessBuilder("ls", "-ltr");
Process process = builder.start();
StringBuilder out = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append("\n");
}
System.out.println(out);
}
}
}
This is not right. Here's how you should use Runtime.exec()
. You might also try its more modern cousin, ProcessBuilder
:
Source: Stackoverflow.com