By user12611203 on Feb 17, 2009
We've recently been discussing this at work, and now that I have a setup working well for me (and working simply), I wanted to document the steps. My goal was to switch JDKs used by all the tools in my enviornment: NetBeans, ant, maven, command line Java, etc., so that they were all using either Java 5 or 6. After some missteps, here's what works for me.
I'm using Mac OS 10.5.6, but this info should be valid for any nearby version. The Mac comes with, and occasionally updates, JDKs 1.4 through 1.6. You can see the versions you have installed in this directory:
CurrentJDKsymbolic links in that directory to switch the JDK my system was using, I have seen the error of my ways and there is an easier solution. There are two steps, listed below:
1. Command Line Java
My Java is coming from
/usr/bin/java, which points off to one of the versions
in the 'Versions' dir described above. To change the version of the JDK you're getting here,
use the Java Preferences application under Applications -> Utilities ->
hostname% java -version java version "1.5.0_16" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284) Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_16-133, mixed mode) hostname% java -version java version "1.6.0_07" Java(TM) SE Runtime Environment (build 1.6.0_07-b06-153) Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_07-b06-57, mixed mode)
2. Scripts and Applications That Use Java
Generally, other built-in applications or one that you install will use the
JAVA_HOME environment variable to pick a JDK. By default, you won't have this
set, and Mac-specific versions of startup scripts will usually create one by using
CurrentJDK link in the Java 'Versions' directory. The steps to
add environment variables are documented in
this article, but
I can save you a little time. Create a directory
.MacOSX in your home
directory and add a file called
environment.plist. Here are the entire
contents of my
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd"> <plist version="0.9"> <dict> <!-- When changing this, also run Java Preferences and change there. --> <key>JAVA_HOME</key> <string>/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home</string> </dict> </plist>
With this value set, all processes started as you will have JAVA_HOME available to them. Since this file is read when you log in, you'll have to log out/in once after you create or edit this file.
Special case: NetBeans
The IDE I use is NetBeans, but the following idea probably applies to other large applications as well. When NetBeans is installed, it will pick a JDK to use and hard code it in a properties file. If you want it to rely on the JAVA_HOME that you're now setting in environment.plist, you just need to edit one file. Edit this file:
Note that, as the netbeans.conf file points out, you can always force a different JDK to be used by specifying it on the command line when starting the IDE. For your copying and pasting pleasure, here is the command to use to start it from terminal (I'm giving the 'help' option in this case). If you're using a different version, autocomplete ought to help with the version part of the path:
/Applications/NetBeans/NetBeans\\ 6.5.app/Contents/MacOS/netbeans --help
To recap, you can switch JDKs for your whole system by using the Java Preferences application along with changing the value in your environment.plist file. To switch on the fly, use the Preferences app and set a new value for JAVA_HOME in whatever terminal you're using, though some apps like NetBeans will still pick up the system value and you should specify the desired JDK on the command line.
Just to get some useful info in one place, here are some links I've found very helpful for people first switching to Mac, whether developer or not.