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

Building OpenJDK 6 on Ubuntu Linux 12.04 LTS

The build process of OpenJDK relies heavily on Unix-like development tools. Linux-based operating systems usually have top notch support for such tools, so building OpenJDK on Linux can be simpler than on Windows. For major distributions such as Fedora or Ubuntu, build toolchain and all dependencies are already included in distributions as packages and can be installed easily.

Ubuntu 12.04 LTS was chosen for this book because it is one of the most popular Linux distributions. For readers running other operating Ubuntu 12.04, virtual images may be found online for the most popular virtualization tools, such as Oracle VirtualBox or VMware.

To build binaries for i586 and amd64 architectures, corresponding versions of Ubuntu should be used. Build instructions are exactly the same for both architectures, so they won't be mentioned further in this recipe.

Getting ready

For this recipe, we will need a clean Ubuntu 12.04 (server or desktop version) running.

How to do it...

The following steps will help us to build OpenJDK:

  1. Install prepackaged binaries of OpenJDK 6:
    sudo apt-get install openjdk-6-jdk
    
  2. Install GCC toolchain and build dependencies:
    sudo apt-get build-dep openjdk-6
    sudo apt-get install libmotif-dev
    
  3. Download and decompress official OpenJDK 6 build 30 tarball:
    mkdir openjdk-6-src-b30-21_jan_2014
    cd openjdk-6-src-b30-21_jan_2014
    wget https://java.net/projects/openjdk6/downloads/download/openjdk-6-src-b30-21_jan_2014.tar.xz
    tar xJf openjdk-6-src-b30-21_jan_2014.tar.xz
    rm openjdk-6-src-b30-21_jan_2014.tar.xz
    
  4. Open the jdk/make/javax/sound/jsoundalsa/Makefile file with your favorite text editor and change line 68 from LDFLAGS += -lasound to:
    OTHER_LDLIBS += -lasound
    
  5. 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 ALT_BOOTDIR=/usr/lib/jvm/java-6-openjdk
  6. Import the environment into the current shell session (note a dot and a space before it):
    . buildenv.sh
    
  7. Start the build process from the openjdk-6-src-b30-21_jan_2014 directory:
    make 2>&1 | tee make.log
    
  8. Wait for the build to finish, and try to run the newly built binaries:
    cd build/linux-amd64/j2sdk-image/
    ./bin/java –version
    openjdk version "1.6.0-internal"
    OpenJDK Runtime Environment (build 1.6.0-internal-ubuntu_22_jan_2014_13_12-b00)
    OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)
    

How it works...

Prepackaged binaries of OpenJDK 6 are required because some of the build steps are run using the external Java runtime.

The build-dep command is used to install all the dependencies that are required to build the specified package. As Ubuntu packaged OpenJDK 6 is quite close to the official OpenJDK 6, this command will install almost all of the required dependencies.

The libmotif-dev package is the only additional package required. It contains the Motif GUI toolkit header files.

The jdk/make/javax/sound/jsoundalsa/Makefile file adjustment is required to conform with the Ubuntu 12.04 GCC 4.6 toolchain. It is not required for the original GCC 4.2 toolchain and may not be required for more recent OpenJDK 6 official sources, because this change is going to be included in OpenJDK 6 upstream.

The tee command is used to write output to the logfile and the screen simultaneously.

After a successful build on the amd64 platform, JDK files will be placed in build/linux-amd64/j2sdk-image and JRE files will be placed in build/linux-amd64/j2re-image. On the i586 platform, the build/linux-i586 path will be used instead.

There's more...

Javadoc generation takes a lot of time and is the most memory consuming step of the build. It may be skipped with an additional environment variable:

export NO_DOCS=true

This build has generated a milestone tag and a build number b00. The predefined build number and milestone may be set using additional environment variables:

export MILESTONE=ubuntu-build
export BUILD_NUMBER=b30

The cacerts file may be provided during the build using an additional environment variable:

export ALT_CACERTS_FILE=path/to/cacerts

For amd64 builds, preinstalled Java provided by the ALT_BOOTDIR variable may be either the amd64 or i586 build. The i586 binaries consume less memory and may be used for amd64 builds on limited hardware.

The build process for OpenJDK 6 is effectively single-threaded; parallel builds (using make -j N) are not supported for this version.

See also