手动编译openjdk11

 

之前一直想要尝试编译jdk。奈何。。。

why

恰好手里有一本书,书上讲解的是编译openjdk7.按照教程发现现在的ubuntu已经是16了,教程还是按照12的来。很多依赖已经找不到了。所以想一步到位,直接到11.

前提准备

  1. 在谷歌云申请了东京 8U30G的服务器,作为编译服务器。系统为Ubuntu 16.03
  2. 为了避免其他问题,使用了root账号。

编译

  • 打开openjdk下载页面http://jdk.java.net/11/
  • jdk源码使用Mercurial 进行版本管理。首先安装Mercurial
  • 执行apt install mercurial报错。百度发现系统第一次安装依赖需要执行apt-get update
  • 继续执行apt-get install mercurial
  • 继续执行 hg clone http://hg.openjdk.java.net/jdk-updates/jdk11u/
  • 克隆源码很慢,东京的服务器跑了半个小时,如果切换到美国会更快。
  • 切换分支hg update jdk-11.0.1+13
  • 安装jdk10 编译jdk需要Bootstrap JDK wget https://download.java.net/java/GA/jdk10/10.0.2/19aef61b38124481863b1413dce1855f/13/openjdk-10.0.2_linux-x64_bin.tar.gz,tar -zxvf openjdk-10.0.2_linux-x64_bin.tar.gz。因为jdk源码有c代码也有java代码。(鸡生蛋,蛋生鸡)(注意不能设置环境变量)。
  • 安装其他依赖apt-get install -y autoconf zip libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev libcups2-dev libfontconfig1-dev libasound2-dev
  • 编译的时候会用到Bootstrap JDK的jre目录下的lib库,我们这里只有JDK没有jre,因此需要创建一个jre目录,再把jdk的lib文件夹复制到这个目录下,执行cp -r lib jre
  • 源码会下载到本地jdk11u的目录中
  • cd jdk11u
  • 执行bash configure --with-num-cores=8 -with-memory-size=22192 --with-boot-jdk=../jdk-10 8是8核,22192是MB内存 jdk-10是Boostrap JDK的路径 一段时间(10s)后会显示如下信息:
====================================================
The existing configuration has been successfully updated in
/root/jdk11u/build/linux-x86_64-normal-server-release
using configure arguments '--with-num-cores=8 -with-memory-size=22192 --with-boot-jdk=../jdk-10'.

Configuration summary:
* Debug level:    release
* HS debug level: product
* JVM variants:   server
* JVM features:   server: 'aot cds cmsgc compiler1 compiler2 epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services vm-structs'
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 11.0.1-internal+0-adhoc.root.jdk11u (11.0.1-internal)

Tools summary:
* Boot JDK:       openjdk version "10" 2018-03-20 OpenJDK Runtime Environment 18.3 (build 10+44) OpenJDK 64-Bit Server VM 18.3 (build 10+44, mixed mode)  (at /root/jdk-10)
* Toolchain:      gcc (GNU Compiler Collection)
* C Compiler:     Version 5.4.0 (at /usr/bin/gcc)
* C++ Compiler:   Version 5.4.0 (at /usr/bin/g++)

Build performance summary:
* Cores to use:   8
* Memory limit:   22192 MB

WARNING: The result of this configuration has overridden an older
configuration. You *should* run 'make clean' to make sure you get a
proper build. Failure to do so might result in strange build problems.

  • 执行make 开始编译,等待编译完成后控制台输出以下类似信息。这个步骤持续了7分27秒,cpu load 会飙到12(机器8核),但内存大概使用四分之一的样子。
Creating support/modules_libs/jdk.management.agent/libmanagement_agent.so from 1 file(s)
Creating support/modules_libs/jdk.net/libextnet.so from 1 file(s)
Creating support/modules_libs/jdk.pack/libunpack.so from 7 file(s)
Creating support/modules_cmds/jdk.pack/pack200 from 1 file(s)
Creating support/modules_cmds/jdk.pack/unpack200 from 7 file(s)
Creating support/modules_cmds/jdk.rmic/rmic from 1 file(s)
Creating support/modules_cmds/jdk.scripting.nashorn.shell/jjs from 1 file(s)
Creating support/modules_libs/jdk.sctp/libsctp.so from 3 file(s)
Creating support/modules_libs/jdk.security.auth/libjaas.so from 1 file(s)
Compiling 4 files for BUILD_JIGSAW_TOOLS
Stopping sjavac server
Finished building target 'default (exploded-image)' in configuration 'linux-x86_64-normal-server-release'
  • 在jdk11u目录下存在一个build文件夹。
  • build/linux-x86_64-normal-server-release目录下,有个jdk目录,这里面就是最新构建的OpenJDK,进入里面的bin目录,再执行命令./java -version,可见最新的版本信息如下.
root@instance-2:~/jdk11u/build/linux-x86_64-normal-server-release/jdk/bin# ./java -version
openjdk version "11.0.1-internal" 2018-10-16
OpenJDK Runtime Environment (build 11.0.1-internal+0-adhoc.root.jdk11u)
OpenJDK 64-Bit Server VM (build 11.0.1-internal+0-adhoc.root.jdk11u, mixed mode)

以下是使用官方编译好的jdk的版本信息。

$java11/java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
  • SUCCESS

其他测试

重新申请了美国的机器64核57.6GB的内存,实验发现我要求使用64核,实际使用了21核。

====================================================
The existing configuration has been successfully updated in
/root/jdk11u/build/linux-x86_64-normal-server-release
using configure arguments '--with-num-cores=64 -with-memory-size=22192 --with-boot-jdk=../jdk-10.0.2'.
Configuration summary:
* Debug level:    release
* HS debug level: product
* JVM variants:   server
* JVM features:   server: 'aot cds cmsgc compiler1 compiler2 epsilongc g1gc graal jfr jni-check jvmci jvmti managem
ent nmt parallelgc serialgc services vm-structs'
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 11.0.1-internal+0-adhoc.root.jdk11u (11.0.1-internal)
Tools summary:
* Boot JDK:       openjdk version "10.0.2" 2018-07-17 OpenJDK Runtime Environment 18.3 (build 10.0.2+13) OpenJDK 64
-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)  (at /root/jdk-10.0.2)
* Toolchain:      gcc (GNU Compiler Collection)
* C Compiler:     Version 5.4.0 (at /usr/bin/gcc)
* C++ Compiler:   Version 5.4.0 (at /usr/bin/g++)
Build performance summary:
* Cores to use:   21
* Memory limit:   22192 MB
WARNING: The result of this configuration has overridden an older
configuration. You *should* run 'make clean' to make sure you get a
proper build. Failure to do so might result in strange build problems.

而且,全量编译仅用时2分40s,cpu load 最高到19多。

下载源码用时7min左右,相当的快。