I am using ImageMagick
on Mac OS X (10.7). I installed it with the help of MacPorts
.
When I now enter the Terminal and write:
identify image.jpg
it is working perfectly fine.
But now while executing it from within Java, the following exception gets thrown:
org.im4java.core.CommandException: java.io.FileNotFoundException: identify
I can see it's on the PATH
by running:
which identify
with the response:
/opt/local/bin/identify
Now while running:
echo $PATH
I get the response:
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
The same code works perfectly on Windows where ImageMagick
is also installed.
So why is im4java not finding identify
in the PATH
at all?
Like it's described here for OS X 10.8 and here for OS X 10.7 the only complete solution is to set your PATH
in /etc/launchd.conf
.
Per default the PATH
for Applications ist set to /usr/bin:/bin:/usr/sbin:/sbin
, even if you do not have a /etc/launchd.conf
at all.
So you have to do the following in your terminal:
sudo vi /etc/launchd.conf
and add the following line or modify it, if it already exists:
setenv PATH /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
Important: Now you need to reboot your Mac!
You can reproduce your PATH
in your Java application with the following code:
public class Main {
public static void main (String[] args) {
System.out.println("PATH=" + System.getenv().get("PATH"));
}
}
There is a second solution, if you start your Program from within an IDE like Eclipse you can set the PATH there as well. In Eclipse you can do that via Run | Run Configurations | Environment
while selecting your launch configuration on the left side bar under Java Application
.
I did reproduce it with the following code and image.jpg
located in ${user.dir}
aka the current directory where your Java app got launched from.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Main {
public static void main (String[] args) {
System.out.println("PATH=" + System.getenv().get("PATH"));
try {
Process exec = Runtime.getRuntime().exec("identify image.jpg");
InputStream is = exec.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
You should get similar output like this after running the code above:
PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
image.jpg JPEG 690x518 690x518+0+0 8-bit sRGB 152KB 0.000u 0:00.000
The first output line show your PATH
for the Java application you run right now.
The second output line comes from identify image.jpg
.
Note: I am running Mac OS X 10.8.2 and MacPorts 2.1.3
Note: There were a way prior to Mac OS X 10.8 to set global variables on a user by user base employing ~/.MacOSX/environment.plist
. But this is no longer working anymore starting with Mountain Lion (aka Mac OS X 10.8). Details can be checked out here:
Try setting the search path to the target directory:
import org.im4java.process.ProcessStarter;
ProcessStarter.setGlobalSearchPath("/opt/local/bin");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With