I am using jhead
to check if an image has an Orientation flag set, and if it is, then rotate it and set the exif information to indicate it does not need to be rotated when viewed. On the commandline it looks like:
jhead -autorot 'IMG_3680.JPG'
I am trying to use ProcessBuilder
to call this from my java app on the images I am looking at, but it uses jpegtran
to do the actual image rotation. Both these apps work correctly from the commandline and are located in /opt/local/bin
on my mac.
I keep getting:
sh: jpegtran: command not found
Error : Problem executing specified command
in file '/images/IMG_3681.JPG'
My code is:
public static void main(String[] args) throws Exception {
File[] files = (new File("/images")).listFiles();
for (File file : files){
ProcessBuilder pb = new ProcessBuilder("jhead", "-autorot", file.getAbsolutePath());
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process p = pb.start();
}
}
Do I need to provide a hint to ProcessBuilder
in order for jhead
to be able to call jpegtran
?
After some guess work, I determined that the PATH
environment variable was not set correctly. It had /usr/bin:/bin:/usr/sbin
which obviously does not include /opt/local/bin
. I added it like so:
Map<String, String> env = pb.environment();
env.put("PATH", env.get("PATH")+":/opt/local/bin/");
Which now produces the correct result. The real solution though is to add /opt/local/bin/
to the PATH
environment variable and not add this in the code directly.
PATH
variable is set correctly in Terminal, but I had to add it to Eclipse. To add it to Eclipse:
PATH
and Value enter /usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin/
If you do need to set this in OS X then there seems to be a few ways (or really a few places). If you need it for applications that are launched via Spotlight then go here. If you need to set if for Terminal than go here.