OpenJDK Cookbook
上QQ阅读APP看书,第一时间看更新

Building OpenJDK 7 on Mac OS X

OpenJDK 7 supports the Mac OS X platform as a first class citizen and building it using the proper version of toolchain is almost as easy as on Linux.

Historically, Java had first class support on Mac OS X. JDK was based on Sun codebase but built by Apple and integrated fully into their operating system environment. Up to Mac OS X 10.4 Tiger, graphical user interface applications written using the standard Swing toolkit had access to most of the Cocoa native interface features. Applications written in Java were very close to native ones, while still being cross-platform.

But with the next releases, the level of Java support went down. Starting from Mac OS X 10.5 Leopard, newer Cocoa features became unsupported for Java. Release of Apple Java 6 was postponed (comparing to Sun releases for other platforms) for more than a year. Java 6 was released in December 2006 but was not available for Mac OS X users until April 2008. Finally in October 2010, Apple officially announced discontinuation of Java support. Apple Java 6 is still being updated with security updates and may be installed on Mac OS X 10.9 Mavericks (the latest version at the time of writing) but no future Java versions will be released by Apple.

Third-party open source Java distributions existed for Mac OS X. The most notable is the SoyLatte—X11-based port of the FreeBSD Java 1.6 patchset to Mac OS X Intel machines. SoyLatte predated OpenJDK, was licensed under Java Research License, and supported Java 6 builds. Now it is part of the OpenJDK BSD-Port project.

Currently the official latest stable Java version for Mac OS X is Oracle Java 7 which matches closely with OpenJDK. In this recipe, we will build an OpenJDK 7 update 40 on Mac OS X 10.7.5 Lion. This operating system version was chosen because 10.7.3 is the official minimum build requirement platform that should provide the most compatible binaries for Mac OS X, and 10.7.5 matches it quite closely, but may run on newer Intel Ivy Bridge processors and may also be virtualized relatively easily using popular virtualization tools such as Oracle VirtualBox or VMware.

Getting ready

For this recipe, we will need clean Mac OS X 10.7.5 Lion running.

How to do it...

The following procedures will help us to build OpenJDK 7:

  1. Download Xcode 3.4.2 for Lion (March 22, 2012) from https://developer.apple.com/xcode/ (an Apple developer's account is required, registration is free) and install it.
  2. Download the Command Line Tools for Xcode—late March 2012 (March 21, 2012) using the same download link mentioned previously and install it.
  3. Run this command from the terminal to set up the command line tools:
    sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer/
    
  4. Navigate to Applications | Utilities and run X11.app to install it as an additional download.
  5. Install the JDK 7—Oracle distribution, or prebuilt OpenJDK binaries may be used.
  6. Download Apache Ant 1.8.4 from http://archive.apache.org/dist/ant/binaries/ and decompress it.
  7. Download the source archive openjdk-7u40-fcs-src-b43-26_aug_2013.zip from http://download.java.net/openjdk/jdk7u40/promoted/b43/ and decompress it.
  8. Create a new text file buildenv.sh with the following environment settings:
    export LD_LIBRARY_PATH=
    export CLASSPATH=
    export JAVA_HOME=
    export LANG=C
    export ANT_HOME=path/to/ant
    export PATH=path/to/ant/bin:$PATH
    export ALT_BOOTDIR=path/to/jdk7
  9. Import the environment into the current terminal session (note a dot and a space before it):
    . buildenv.sh
  10. Start the build process from the openjdk directory:
    make 2>&1 | tee make.log
  11. Wait for the build to finish, and try to run the newly built binaries:
    cd build/macosx-x86_64/j2sdk-image/
    ./bin/java –version
    openjdk version "1.7.0-internal"
    OpenJDK Runtime Environment (build 1.7.0-internal-obf_2014_02_07_21_32-b00)
    OpenJDK 64-Bit Server VM (build 24.0-b56, mixed mode)

How it works...

As well as the Xcode Command Line Tools that are used for the main part of the native source, Xcode itself is also required to build platform specific code.

OpenJDK on Mac OS X is moving away from using the X11 server, but it is still required for Version 7 builds. Mac OS X 10.7 Lion has X11 preinstalled, it just needs to be run once to be configured for the build.

Apple JDK6 may be used instead of OpenJDK7, but it requires additional configuration.

The Apache Ant build tool is required for some modules of the build.

There's more...

This recipe uses the official Apple builds of GCC (and G++) Version 4.2 as compilers. After that version, official Apple support for GCC was discontinued for licensing reasons. Clang—the open source compiler initially developed by Apple—is the default and preferred compiler in newer versions of Mac OS X. While newer versions of OpenJDK support Clang on Mac OS X, Version 7 still requires GCC.

OpenJDK 7 could be built on Mac OS X 10.8 Mountain Lion using the same steps. The only addition is that the X11 server should be installed separately using the XQuartz project.

Newer versions of Xcode and recent updates of Mac OS X 10.9 Mavericks may break the OpenJDK builds. If builds using newer OS / toolchain are desired, it is better to check the current situation with the build and proposed solutions on OpenJDK mailing lists.

See also

  • The Preparing CA certificates recipe from Chapter 2, Building OpenJDK 6
  • The Building OpenJDK 6 on Ubuntu Linux 12.04 LTS recipe from Chapter 2, Building OpenJDK 6 for information about build tuning
  • The official build instructions for Mac OS X on OpenJDK Wikipedia at https://wikis.oracle.com/display/OpenJ