stys
stys

Reputation: 727

Cross-platform way to run external process from java?

I need to call an external program from Java such as ImageMagick's convert. It fails to work on Windows unless I put cmd /c before the actual command.

String source = "test.jpg";
String result = "test-thumbnail.jpg"; 
ProcessBuilder builder = new ProcessBuilder().command("cmd", "/c", "convert", source, "-thumbnail", "295x", result);
Process process = builder.start();

How to avoid using cmd /c so that my code works on OS other than Windows?

Without cmd /c I get into very similar problem as described here: running imagemagick convert (console application) from python - that there exists a native Windows convert.exe which is being called rather than ImageMagick's convert.exe. It seems like PATH is not picked by environment of the child process.

I have double checked that my system PATH has ImageMagick directory before C:\Windows\system32. Also the command itself runs perfectly fine when I type it into the Windows command line.

Upvotes: 5

Views: 997

Answers (1)

Eduardo Pérez
Eduardo Pérez

Reputation: 488

You can check the OS.

private static String OS = System.getProperty("os.name")

Upvotes: 3

Related Questions