Monday, December 10, 2012

How to install JDK 7 and set up JAVA_HOME in Mac OS X Mountain Lion

The last 2 hours have been exciting! Thanks to the complexities involved in setting up the Java development environment on Mac OS X (10.8.2) code named as Mountain Lion (latest as of today). 

Apple did something nasty after Oracle took over the SUN Microsystems, Apple announced some months back that they are not going to support the further releases of the JDK. As a result the Mac OS now doesn't come with the JRE installed by default. We have to manually install it. If you are a Java developer simply JRE won't suffice, you need to install the JDK (Java Development Kit).

Okay, that's the crappy story behind why this complexity arise now and was not present earlier. Installing JDK 7 is easy on Mac OS X. (Installing JDK 6 is impossible as of now on Mac OS X Mountain Lion, I've done some reading on this and I couldn't find a way - the main reason is Oracle has not released Mac OS compliant JDK 6 installation. If you know installing JDK 6 on Mac OS X, please post it in the comments). The Oracle guide for installing JDK 7 on Mac OS X is pretty clean so I'm not wasting my time in writing something that is already clean and clear. JDK is installed system-wide and you cannot install it on user basis. You also need admin access for this installation. Installing JDK also installs JRE on your system. 

To summarize, the steps are:

1. Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html and download the appropriate .dmg file (for me it was jdk-7u9-macosx-x64.dmg). 

2. The file will take some time to download, it took me like 30 mins. The file will be downloaded in the Downloads folder, can be accessed through Dock. Double click the .dmg file and a Finder window will appear with a box - double click it too and the installation will start. That's it!! A confirmation window will appear when installation is done.

It's that simple! 

What sucks is setting up of $JAVA_HOME!! 

It took me more than an hour to figure this out! It's also because I'm pretty new to Mac OS. 

Okay so where is JDK exactly installed on Max OS? It's here: /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk

And where is the bin folder of the JDK, it's here:
/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/bin

And where the JAVA_HOME should point to? 
/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home

JAVA_HOME should point to the home directory and not the bin folder. 

How to set up JAVA_HOME ? The real reason I'm writing this post...

Step 1: 
Check out whether you already have set up $JAVA_HOME and $PATH. Remember the letters should be capital. Open the terminal and type the following commands
  
echo $PATH 
echo $JAVA_HOME

These will print out the current values of $PATH and $JAVA_HOME. You would probably have some value for your $PATH but it will be blank for $JAVA_HOME. 

Step 2:
$PATH and $JAVA_HOME should be present in your .profile file. This file is present in your home folder - it is the place where your terminal opens by default. If .profile file is not present you have to create it! This is the trickest part but it is simple!! Following are the steps:
  • Open your terminal and type the command vi .profile This will open the .profile file and if not present will create it. 
  • Paste the following in the file
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home;

export JAVA_HOME;

PATH=$PATH:$JAVA_HOME;
export PATH;
          • Write & quit the file by typing :wq   

          Now, try to see the values of the $PATH and $JAVA_HOME as specified in the Step 1. In my case they show the following values: 

          echo $PATH
          /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home

          echo $JAVA_HOME
          /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home

          Step 3:
          Verifying everything. Just once command will verify everything: 

          $JAVA_HOME/bin/java -version

          If this command gives you the Java version that you installed, then you are good to go! In my case it prints out
          java version "1.7.0_09"

          That's it!! Wasn't it simple? :)

          12 comments:

          1. How do you write a program in the text editor.

            ReplyDelete
            Replies
            1. I have already detailed it in Step.2. The text editor I mentioned is vi. Check the following lines in Step.2.
              "Open your terminal and type the command vi .profile This will open the .profile file and if not present will create it. "

              Thanks for stopping by!

              Delete
          2. I simply removed the symlink /Library/Java/Home and created a new one with the same name pointing to the new version.

            ReplyDelete
          3. Gracias, lo estaba buscando como loco hace tiempo; ahora puedo configurar Pentaho en mi Mac!
            Thanks, I was looking like crazy for some time; and now I can configure Pentaho on my Mac!

            ReplyDelete
          4. Not Setting java home environment variable permanently.. when i close terminal and restart then java bash error shown

            ReplyDelete
            Replies
            1. I'm experiencing the same scenario : "Not Setting java home environment variable permanently.. when i close terminal and restart then java bash error shown"

              Delete
          5. In step 2 on my OSX 10.9, I had to "vi .bash_profile" instead of vi .profile

            don't know why though...

            ReplyDelete
          6. Thanks much this really helps!

            ReplyDelete
          7. Great tutorial! Really helpful THANKS

            But I set up the environment variables in .bash_profile instead of .profile.

            ReplyDelete