Sunday, 16 December 2012

Add the sun-provided platform specefic extensions to the apple-provided JDK source jar

Add the sun-provided platform specefic extensions to the apple-provided JDK source jar
The problem

The apple-provided JDK for mac osx systems, i.e. the developer version source JAR file doesn't contain the required sources for the platform-specific extensions provided by sun. When inspecting or debugging the JDK code via an IDE then that will result in displaying the decompiled version of these classes instead of the actual sources that should be shipped with the JDK distribution. However, the same sources for these extenstion packages (sun.*) are available from within the OpenJDK6, all what we need to do is to download/clone a copy of the OpenJDK6 sources and copy these over to the apple-provided JDK source JAR file to have the actual sources being displayed in your IDE while inspecting/debugging code.

How to do it?
  1. Navigate to the home directory where your apple-provided JDK is installed, you can do so using the following command:
    cd `java_home -v 1.6`
  2. Rename the original source JAR file that contains the source codes missing the sun.* platform extension packages, you can do so using the following command:
    sudo mv src.jar src.jar.orig
  3. Check out the latest copy of the OpenJDK6 sources, you can obtain a copy of the OpenJDK6 sources either from a ZIP file or via clonning a Mercurial repository, here we we will clone the latest copy from a Mercurial repository using the following command:
    hg clone
  4. Now we need to make a copy of the original source JAR provided by apple and extract it, later on we will add the missing sources into it before copying back into our JAVA_HOME directory
    cp src.jar.orig ~/tmp
    cd ~/tmp
    jar -xvf src.jar.orig
  5. Now we need to copy the missing sun packages from the OpenJDK sources into the content of the extracted jar, as follows:
    cp -R >The directory to your OpenJDK6 sources>/jdk/src/share/sun .
  6. After copying the missing sources we need to re-build the sources jar and move it back again into the JAVA_HOME where the apple-provided JDK is installed, we need to also make sure to restart our IDE after copying the new JAR for the changes to take effect.
    jar -cvf src.jar *
    sudo cp src.jar `java_home -v 1.6`

Saturday, 1 December 2012

Build the OpenJDK8 in 10 easy steps on Mac OSX

Build the OpenJDK8 in 10 easy steps on Mac OSX
Introduction into the OpenJDK

Java OpenJDK is an attempt to have all the development for the reference implementation of the java platform being done in the open and to create a community around the development process comprised from the day to day working Java developers which would help to have better standard, a user-targeted API and an early feedback of usability and ease of use fed into the JDK community.

It will also allow the day to day developer to interact and hack with the OpenJDK as its being developed day after the other. The current release of the OpenJDK is the JDK8. The sources of the open JDK is being hosted on a Mercurial repository and it is publicly available for download.

Cloning the JDK repository is done on a read-only Mercurial repository, while writing (committing changes) is done on a read-write repository called the gate repository. Changes are autmatically pushed from the gate to the read-only repository. Each JDK release consists of multiple projects, each project has several components (or repositories). The collection of repositories a project comprised of is called a forest. As such, its advisable to clone the whole set of JDK forest when working on a particular project rather than a single project's repository for the sake consistency.

The naming scheme that the JDK repository follow is as below:

This article is aimed to be a simple step by step guide to build the OpenJDK8 on Mac OSX systems with a little insight on the build process tools and techniques


The following is a list of the required software and tools for building the OpenJDK

  1. Mac OSX 10.7.3 or later

  2. Having the latest version of Xcode installed on your mac along with the developer tools. On my machine, I had the 4.3.2 version installed.

  3. Since the JDK source code is hosted on Mercurial repository, we need to have the hg Mercurial tool installed, the required version should be 0.9.4 or newer, on my machine I had the 2.2.2 version installed.(to know which version of hg installed on your system, run the following command: hg -version)

  4. An earlier version of the JDK, preferably JDK7.

Build instructions
  1. install the bootstrap JDK:

    The OpenJDK build process requires the existence of an earlier version of the JDK to be used as the bootstrap JDK during the build. The preferred version of the JDK to be used as a bootstrap JDK is the JDK7, but an earlier version such as JDK6 should suffice. To make sure that you have the latest version of JDK installed, run the following command:
    /usr/libexec/java_home -v 1.7 --exec java -version

  2. unset the JAVA_HOME:

    The build process requires that we unset the JAVA_HOME directory from the shell within which we are running the JDK build process, to do that run the following command:
    unset JAVA_HOME

  3. set the ALT_BOOTDIR:

    At this point we need to set the ALT_BOOTDIR environment variable to point to the version of the JDK that we like to use as the bootstrap JDK:
    export ALT_BOOTDIR=`/usr/libexec/java_home -v 1.7`

  4. Clone the JDK sources:

    Now it's time to download (or clone) the latest sources of the OpenJDK from the Mercurial repository, for that we need to use the hg command as follows to download the top level projects that the JDK is comprised of, but this will not clone the nested repositories contained in each project:
    hg clone

  5. Clone the nested repositories:

    Now we need to clone all the nested repositories within the JDK, for that we need to run the shell script as follows:
    chmod u+x # adding the execute permission 
    On my machine it took approximately 25 minutes to download the whole source code of all the open JDK repositories from the Mercurial repos, and the whole size of the JDK sources is about 735 MB

  6. pull the latest updates:

    in the case that we are building from an out of date clone of the JDK sources, then we need to pull the latest updates before attempting to build it, while we can do that using the Mercurial command line but it will be tedious to do that for every single nested repo, hence we have a helper script that can do that for us in one go:
    sh ./make/scripts/ pull -u

  7. Build the JDK

    Now it is the time to start building the OpenJDK, the build is managed using the GNU make as follows:
    chmod u+x configure # Adding the execute permission to the current user
    ./configure # Running the configure script
    make sanity # run the sanity checks
    make # Start the actual build, this will also run the sanity checks

  8. install the newly built JDK:

    Now the build process has been completed, its time to move the newly built JDK bundle into the JavaVirtualMachines directory:
    cd ./build/macosx-x86_64/j2sdk-bundle
    sudo cp -R jdk1.8.0.jdk /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk

  9. Verify the version of the newly installed JVM

    /usr/libexec/java_home -v 1.8 --exec java -version
    /usr/libexec/java_home -v 1.8 --exec javac -version

  10. Switch to the newly installed JDK:

    to switch to the newly created JVM, you can set your /usr/libexec/java_home environment variable to the directory where the new JVM has been installed:
    export /usr/libexec/java_home=`/usr/libexec/java_home -v 1.8`

Note: The xcodebuild shell script on Mac systems is used to run the xcodebuild() tool using a version of the Xcode developer tool installed on your machine, this script is a wrapper script that is used to run different versions of the xcodebuild basing on the developer tools path settings that can be located and changed using the xcode-select tool, if you happen to upgrade to a newer version of Xcode tools then that path might get reset and cause the xcodebuild to complain about not being able to locate the right directory to run the Xcode utility from which would cause your build to fail, here is how you can set it to the right path and resume the build:
xcode-select -switch  e.g. /Applications/
to make sure that you have the right path for the Xcode developer tools set, run the following command:
xcode-select --print-path