Java 7 Preview for Mac OS X
Oracle announced the availability of Java SE 7 in beta form for Mac OS X at JavaOne earlier this month. Here, I'll show you how to get started using Java SE 1.7 on your Mac today, while still maintaining your Java SE 1.6 installation as well.
First, download the Java 7 disk image (.DMG file) here: http://jdk7.java.net/macportpreview/. Next, follow these instructions, which are also posted on the download page:
- Create the directory /Library/Java/JavaVirtualMachines, if it does not exist already, via this command:
> sudo mkdir /Library/Java/JavaVirtualMachines
- Open the Java 7 DMG file you downloaded above.
- When the installer opens, simply drag the icon on the left to the JavaVirtualMachines folder within the installer window (see screen shot below). In order for the installer to write to the folder, you'll need to authenticate by providing your password.
- Verify that both Java 6 and Java 7 are installed on your machine by doing the following:
- Browse to the Java 7 folder in the Finder within the /Library/Java/JavaVirtualMachines folder you created (see screen shot below).
- Double-click the icon labeled "JDK 1.7.0 Developer Preview", which will open Java Preferences. Alternatively, you can find Java Preferences in the Applications/Utilities folder.
- You should see your previously installed versions of Java within this Window, as well as the Java SE 7 Preview entries, one for 32bit and the other for 64bit (see screen shot below).
- Browse to the Java 7 folder in the Finder within the /Library/Java/JavaVirtualMachines folder you created (see screen shot below).
The entry at the top of the list will be the default version of Java chosen when you execute it from the command line. You can leave Java SE 7 where it is, or make it the default by moving it up to the top of the list.
Build Java SE 7 Applications
To use the Java 7 installation for new projects, you'll need to create a new Java platform within your favorite IDE. In NetBeans, for example, choose "Java Platforms" from the Tools menu, click the Add Platforms button, and navigate to the Java 7 Preview folder. The full path should be /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home (see screen shot below).
Click the Next button, provide a name for the platform (the default should be "JDK 1.7"), and then click Finish. From now on, you can choose the JDK 1.7 platform for new or existing projects by changing the platform in the project Properties window. To do this, right-mouse click on the project name, choose "Properties" from the popup menu, choose "Libraries" from the Categories section on the left, and then click on the Manage Platforms button on the right. There should be an entry for each Java platform, including the one you just created (see screen shot below).
For Eclipse, the process is similar. To begin, open the Preferences window, expand the "Java" entry within the list on the left, and click on "Installed JREs". You should see your existing JRE entries; click the Add button on the right to add a new one. As for JRE type, choose "Standard JRE," and then click Next. In the next window, provide the path to Java SE 1.7 (Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home), and provide a name, i.e. "JDK 1.7".
Click Finish, then OK, and you're all set. You can check the checkbox for the new JDK if you want it to be the default for new projects. To change the JDK for existing projects, you'll need to update the Java compiler and JDK used in the project's properties much like you would for NetBeans.
If you didn't make Java 1.7 the default in Java Preferences, you'll need to provide the explicit path to Java 7 at the command line. For instance:
> /Library/Java/JavaVirtualMachines/JDK\ 1.7.0\ Developer\ Preview.jdk/Contents/Home/bin/java -version
That looks terrible, and it's a pain to type even as you tab through to automatically fill in the path names. I suggest creating an environment variable, via the following command, and adding it to /etc/profile:
export JDK7="/Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home/bin/"
Then just type "$JDK7/java" -version, or the command-line for your application, to launch Java SE 7.
Now you're ready to explore the world of Java SE 1.7, with its new Fork/Join framework, File APIs, and lots more. Keep checking here for the latest on Java 7 programming.
Happy coding!
-EJB

