Install Java 6,7,8 on Mac OS X

Here’s a quick guide to have Java versions 6,7,8 installed on Mac OS X above and beyond.

Java 6

Java 6 is the last supported version provided by Apple. Therefore we’ll follow the Apple way to install the JDK although Java 1.6 can be branched out from Oracle or the OpenJDK project.

Successful installation will place Java 6 under /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/. Following that you can mark JAVA_HOME or point your IDE towards /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/. Also to browse the JDK under an IDE point your editor to the JDK sources found in /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home/src.jar

Java 7

We need to go out in the wild to get JDK 7 installed on Mac OS X since mother-Apple doesn’t support it. We can download it either from Oracle or OpenJDK:

Successful installation will place Java 7 under /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/. Following that you can mark JAVA_HOME or point your IDE towards /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/. Also to browse the JDK under an IDE point your editor to the JDK sources found in /Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home/src.jar

Java 8

We can get a copy of the latest snapshot of JDK 8 to play around with the lambda expressions (natively supported on IntelliJ IDEA) if you haven’t tried already closures in Groovy, Scala or just Predicates/Functions in Google Guava from this download resource

Successful installation will place Java 8 under /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/. Following that you can mark JAVA_HOME or point your IDE towards /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/. Also to browse the JDK under an IDE point your editor to the JDK sources found in /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/src.jar

Java 7 in Maven Project

If you have both jdk6 and jdk7 installed and you would want to interchange between the two compilers in a maven project, this can be easily done by the maven-compiler-plugin and its source and target parameters.

Assuming that we have created a JAVA_7_HOME environment variable we can point the maven-compiler-plugin to the jdk7 compiler as follows:

  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<version>2.3.2</version>
  			<configuration>
  				<source>1.7</source>
  				<target>1.7</target>
  				<encoding>${project.build.sourceEncoding}</encoding>			
  				<executable>${env.JAVA_7_HOME}</executable>
  			</configuration>
  		</plugin>

I have also created a 2-module Maven project one for each jdk that you can find in this Github repository.

Installing Java 7 on Mac OS X Lion

Installing Java on Mac OS X used to be a breeze. Once upon a time it was part of the regular Apple update but at some point in midst Java 6 it stopped being supported and now if you want Java 7 you would need to build the jdk first to get the precious binaries.

If you are after only the binaries I found this place where they are made available. Completing the installation is a matter of drag-and-drop and finally jdk 7 will reside in /Library/Java/JavaVirtualMachines/1.7.0.jdk/.

Don’t forget to create/update environment variables or point to that location from your Eclipse, IntelliJ or other IDE. As far as I have checked, Eclipse Indigo 3.7.1 is supporting Java 7 on the fly syntax checker as part of its updated JDT. Older versions of Indigo wouldn’t recognise ie. project coin changes.