Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-platform way to run external process from java?

Tags:

java

windows

exec

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.

like image 620
stys Avatar asked Oct 20 '22 10:10

stys


1 Answers

You can check the OS.

private static String OS = System.getProperty("os.name")
like image 140
Eduardo Pérez Avatar answered Oct 22 '22 03:10

Eduardo Pérez