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: