tags:

views:

6267

answers:

5

I am having a problem running Ant with JDK 1.6 on Mac OS X. Even though Java application versions is set to Java SE 6 in OS X's Java Preference, executing java -version in Terminal also shows java version "1.6.0_07", Ant still seems to use JDK 1.5 to be using JDK 1.5 as it does not see JDK 1.6 classes when compiling my code.

I understand that Ant relies on JAVA_HOME environment variable to specify which JDK to use. However, I do not quite understand how this variable can be set on Mac OS X.

Hence, my question is how to make Ant runs with JDK 1.6 on Mac OS X. If the correct way is still to set JAVA_HOME environment variable, how to set the variable on OS X.

+16  A: 

The JAVA_HOME environment variable is set in your home directory's .profile file. (/Users/ejel/.profile ?) Edit it and set it to what you want it to be. E.g.:

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home

From this point onward, every time you open a new terminal window it will have JAVA_HOME set to this new value. It will not have changed with any existing open windows.

If you are truly aghast to putting this in the profile, or if it conflicts with other software, the export statement could always be run in the terminal manually or go into a script (eg: setj6ev.sh) that is run once before you start running ant tasks.

Stu Thompson
The solution provided is actually straightforward and works. However, the correct path to the Java home directory should be /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
ejel
My bad, didn't test it out, will correct, thankx
Stu Thompson
Ideally you should use .../Versions/Current/Home so that you can then change at a later stage what the VM is without having to update your script - or better still, .../Versions/CurrentJDK/Home if you need to hook into things like e.g. the compiler
AlBlue
@AlBlue: The OP's `./Current/Home` is pointing to Java 5. Your suggestion would not help.
Stu Thompson
+2  A: 

Explicitly setting the JAVA_HOME variable in your .profile/.bashrc/.zshrc isn't actually the recommended way to do it on the mac. There are programs that I've seen get hosed up with an explicitly set JAVA_HOME to a particular version (grails 1.1 with some spring resources for example).

The correct way to set the version of Java that you want to use is to use the /Application/Utilities/Java Preferences.app application.

In there, you drag the version of java that you want to use to the top. This will enable that version for all applications (both those run from the command line and those launched through GUI processes).

You can test the current version by running this from the command line:

java -version

I don't actually like the way that the mac handles the entire set of java symlinked directories and files. It's not obvious and people often screw it up.

See the apple developer page on this for more details.

Ted Naleid
As I mentioned in my question, I have already set the version of Java in JavaPreferences.app. Executing "java -version" shows "1.6.0_07". However, Ant does not seems to rely on this property but rather the JAVA_HOME env var.
ejel
Sorry, I missed that in the original.I thought for sure that this was controlled by the Java Preferences application, but a little testing shows that it isn't.It controls the /System/Library/Frameworks/JavaVM.framework/Versions/Current link, NOT the CurrentJDK link. WTF was apple thinking?
Ted Naleid
A: 

You may need to open a new command prompt instance so that the shell can pick up any changes to the environment variables.

Martin OConnor
+2  A: 

Ted, using the Java Preferences app doesn't change the CurrentJDK symlink in /System/Library/Frameworks/JavaVM.framework/Versions, which is what Ant will use if the JAVA_HOME environment variable isn't set. Thus, you can either change that symlink manually or set the JAVA_HOME environment variable, but if you do neither, then Ant won't use the correct JDK.

You can see the version of the jdk that Ant is using by issuing an <echo message="${ant.java.version}"/> in your build.xml file.

+5  A: 

I've added the line

export JAVA_HOME=`/usr/libexec/java_home`

To my .zshrc file, it seems to do the trick (.bash_profile or whatever if you use bash).

Theo
The question asked about OSX, which doesn't have a /usr/libexec/java_home. In any case, the .zshrc is for zsh; the default shell is bash on OSX.
AlBlue
I'm on OS X, it's there.
Theo