Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing in java code an external program that takes arguments [duplicate]

Tags:

java

process

exec

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?

like image 710
questioner Avatar asked Sep 18 '25 03:09

questioner


1 Answers

Use a p = new ProcessBuilder(params).start(); instead of

p = Runtime.getRuntime().exec(params);

Other than that looks fine.

like image 138
Robert Avatar answered Sep 20 '25 16:09

Robert