Compiling OpenJDK 7 with Ubuntu 12.04 64bit

When Sun Microsystems was still the head of Java, the wise decision to OpenSource it was made. (Thank you, whoever was behind it…) OpenJDK as of today can be built without any binary plugs, so it can be considered as really OpenSource and free. To exercise the liberty of building your own, runnable Java it takes amazingly low effort – that is the topic if this short blog entry.

While consumers and corporate IT seems to prefer Windows as primary operating system, developers tend to choose a unixoid OS: Linux (Debian, Arch, Ubuntu), Apple OSX or OpenSolaris. OpenSolaris lacks a current open source desktop distribution Ubuntu was chosen for this example. To add some complexity Canonical went with a multi-arch setup since Ubuntu 11.04 which looks like being extended/change on Ubuntu 12.04. (This is described on the MultiArch wiki page. We will come back to the problems caused by the transition later in the article.)

In order to retrieve the OpenJDK sources mercurial has to be installed:

sudo apt-get install mercurial

OpenJDK contains many sub-projects, in order to be able to manage the sources separately, the hgforest extension to mercurial is used. Installing it is a little more effort:

  • Clone the hgforest sources: hg clone "$HOME/hgforest"
  • Add the hgforest extension to your mercurial setup by editing ~/.hgrcand adding
  • Please adjust the path accordingly, if you did not clone it to your home directory

As a last preparational step build time library dependencies have to be installed:

sudo apt-get install gawk g++ libcups2-dev libasound2-dev libfreetype6-dev libx11-dev libxt-dev libxext-dev libxrender-dev libxtst-dev libfontconfig1-dev

Now it is time to retrieve the OpenJDK7 sources – use the „7u“ repository in order to retrieve the latest update release sources:

hg fclone openjdk7u

The time depends on your internet connection of course, but it should not take longer than a couple of minutes with a consumer class broadband connection. (Calculate about 1GB of hard disc space for the sources and build output.)

Please make sure that you have a Java compiler installed, it is needed during building OpenJDK. (Of course you can use OpenJDK itself, once it has beend build.) For this tutorial a current Java JDK has been placed at /usr/lib/jvm/jdk1.7.0

After preparation is finished we can build OpenJDK on Ubuntu:

cd openjdk7u
export LANG=C
export ALT_BOOTDIR="/usr/lib/jvm/jdk1.7.0"
export EXTRA_LIBS=/usr/lib/x86_64-linux-gnu/
make sanity && time make

The "ALLOW_DOWNLOADS" parameter is used by the build system to retrieve additional libraries which are required for building and may be missing in the system wide installation. Note the "EXTRA_LIBS" environment entry: This is due to building on a 64 bit Ubuntu using the MultiArch setup. The OpenJDK build system itself does not detect where the alsa sound libraries are installed on this particular setup.

After a while (about 20 minutes, depending on your machine), you should see this:

#-- Build times ----------
Target all_product_build
Start 2012-05-12 11:40:32
End 2012-05-12 12:04:01
00:01:27 corba
00:10:15 hotspot
00:00:18 jaxp
00:00:27 jaxws
00:10:30 jdk
00:00:32 langtools
00:23:29 TOTAL
real 23m30.532s
user 32m10.625s
sys 2m24.617s

The OpenJDK build is created in build/linux-amd64 – you can run your Java from there:

./build/linux-amd64/bin/java -version
openjdk version "1.7.0-internal"
OpenJDK Runtime Environment (build 1.7.0-internal-tkruse_2012_05_11_20_23-b00)
OpenJDK 64-Bit Server VM (build 23.0-b21, mixed mode)

Bonus: OpenJDK 8 is building accordingly. Just substitute the mercurial clone path  with

The build result should look like this:
-- Build times ----------
Target all_product_build
Start 2012-05-12 11:10:35
End   2012-05-12 11:35:00
00:01:30 corba
00:10:30 hotspot
00:00:19 jaxp
00:00:24 jaxws
00:11:08 jdk
00:00:34 langtools
00:24:25 TOTAL

And you can run your own Java 8 now:
./build/linux-amd64/bin/java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-tkruse_2012_05_12_11_10-b00)
OpenJDK 64-Bit Server VM (build 24.0-b09, mixed mode)

As a side note: If you are using Java Native Access (JNA) on an Ubuntu 12.04 64bit machine and you have problems with libraries not being detected properly, update to JNA 3.4, fixes have been incorporate there.