Java 7 on Mac OS X
Note: I’m generally a fan of Apple and own many of their products. My primary computer is a MacBook Pro, which I wouldn’t trade for anything. But Apple has really screwed this one up.
Apple has hurt its users who develop in Java by declaring an end to Java support but continuing to update Java 6 using its automatic update program. This means that installing Oracle Java 7 JDK is a hassle to begin with, and if you blindly accept all of Apple’s software updates (that is, you don’t remember to uncheck Java updates when they appear) you’ll have to re-do parts of your Java 7 installation from time to time because Apple’s Java updates reset all the symlinks to point to its own Java 6. In any case, here’s what you need to do to install and use Java 7 on Mac OS X and fix Apples “updates” if they slip by you. At the time of this writing the current Java 7 is update 17 (jdk1.7.0_17).
First Time Java 7 Installation
Get the Java 7 JDK (not JRE) from here: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html. You want the Mac OS X x64 download (I couldn’t provide a direct link becuase the page I linked in the previous sentence requires you to accept a license agreement). It’s a .dmg (disk image) containing a package installer. Double click the package installer, enter your password when prompted, and your new Java 7 JDK will be installed in a few minutes. Kind of. This step only places the JDK on your hard disk. Now you have to set a few symlinks so that you can actually use it.
To understand the remaining steps you need to understand that Mac OS X is a Unix operating system, and you have to know a little Unix to get certain things done. You may want to read some background information first.
Figuring Out if You Have Apple’s Java 6
Apple installs its JDK in /System/Library/Java/JavaVirtualMachines. If you have Apple’s JDK and the symlinks are set up for it, perhaps after you’ve followed these instructions but then allowed Apple’s Software Update to install a Java update, you can find out with java -version or by seeing where the symlink at usr/bin/java points. If java -version prints something like “Java 1.6” (the important part being the “1.6”) or the symlink at usr/bin/java points to /System/Library/Java/JavaVirtualMachines (note that Apple installs under /System/Library, Oracle installs under /Library), then you need to follow the rest of these instructions.
If you use an older version of Java, like Apple’s Java 6 and try to write programs that use Java class files that were compiled with a newer version of Java you may see an error like this:
warning: ./Location.class: major version 51 is newer than 50, the highest major version supported by this compiler. It is recommended that the compiler be upgraded.
This message means you’re using Java 1.6 (version 50) with pre-compiled .class files that were compiled with Java 1.7 (version 51). In general, you can use the Java disassembler, javap to find out which version of Java was used to compile a class:
Note: the ‘$’ character is the shell prompt. In the example shell interactions in this guide, the commands you type will appear after a shell prompt.
$ javap -verbose Location.class | head Classfile /Users/chris/Downloads/PacmanSkeleton/Location.class Last modified Mar 8, 2013; size 1241 bytes MD5 checksum 2e22b98aa3c1fb2bb3a06e5cd4f2fd24 Compiled from "Location.java" public class Location SourceFile: "Location.java" minor version: 0 major version: 51 flags: ACC_PUBLIC, ACC_SUPER Constant pool:
Note that I piped the output of javap -verbose through head because it prints a ton of information.
Removing Apple’s Java 6
You don’t have permission on the directories that contain the JDK and the system’s java command. So you’ll need to execute your commands that modify these directories as the root user, a.k.a. the superuser. You can do that on a per-command basis by prepending your commands with sudo. These instructions have sudo prepended, so you can simply copy and paste them into your command line. Note that sudo will ask you for your password and it will not be echoed as you type it. Just type it and press ENTER.
Remove Apple’s JVMs:
$ sudo rm -rf /System/Library/Java/JavaVirtualMachines/
Remove installer records:
$ sudo rm /private/var/db/receipts/com.apple.pkg.JavaForMacOSX*
Remove intaller receipts by editing /Library/Receipts/InstallHistory.plist and removing any <dict>...</dict> entries that contain references to Apple’s Java. You can recognize these dict entries because they’ll have child elements that contain com.apple. You can leave Oracle’s installation receipts alone. It’s a bit tedious editing this file. I found the dict elements by searching for ava. Note that you’ll need to edit this file as the superuser, for example by doing:
$ sudo emacs /Library/Receipts/InstallHistory.plist
Once you’ve removed all these traces of Apple’s Java 6 install, Apple’s software update should not (re)install Java 6 and you should only need to reset your symlinks when you install a new JDK from Oracle.