Executing in java code an external program that takes arguments
19,188
Solution 1
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:
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);
}
Solution 2
Use a p = new ProcessBuilder(params).start();
instead of
p = Runtime.getRuntime().exec(params);
Other than that looks fine.
Solution 3
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.
Author by
questioner
Whether you think you can or you think you can’t - you’re right
Updated on June 05, 2022Comments
-
questioner almost 2 years
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.
What to improve in order to make this code working?
-
Michael Mrozek almost 14 yearsWhy should he use ProcessBuilder to execute a single process?
-
questioner almost 14 yearsno, it writes to the console, some float numbers
-
Andrei Fierbinteanu almost 14 yearsMost likely readLine() is called before p has actually outputted anything, and returns null, causing your program to end. You need to wait for p. p.waitFor() might work like Maurice suggested, but I tried that on my example and it just hangs at that call (doesn't return). You might also try using Thread.currentThread().wait(10); before the while loop and see if anything changes (if it does it's definitely a problem with synchronization).
-
questioner almost 14 yearsi make the result float and it equals: -1.0737415E9
-
Robert almost 14 yearsProcessBuilder was written to replace Runtime.exec and makes it easier to customize the process, and provides better control over starting the process. Since there is nothing else actually wrong with his code, seemed as good an improvement as any
-
Muhammad Ahmad Afzal almost 14 yearsWhy make it a float? anyway, this is the output status of prog.exe; it's signification depends on the program being run.
-
questioner almost 14 yearsyep, and it was error code no need(in my case for too many thread) but now it's working i don't know what happened but waitFor(); and Thread.sleep() was useful in investigation