准备工作

  1. Ubuntu版本是16.04.5 LTS;
  2. 确认OpenJDK11源码已经下载到本地,下载方式可以参考文章《Ubuntu环境下载OpenJDK11源码》《利用Docker极速下载OpenJDK11源码》
  3. 安装OpenJDK10作为boot JDK,安装过程可以参考文章《Ubuntu下安装OpenJDK10》

重要文件夹的位置

  1. OpenJDK11源码解压后是个名为jdk11的文件夹,位于此目录下:/home/willzhao/work/compileopenjdk
  2. OpenJDK10安装完成后,整体上是个名为jdk-10的文件夹,位于此目录下:/usr/lib/jvm,因此boot JDK的完整目录是/usr/lib/jvm/jdk-10

以上是我这边的文件路径,如果您和我的不一致也没关系,后面注意修改成您自己对应的即可;

正式开始

  1. 编译的时候会用到boot JDK的jre目录下的lib库,我们这里只有JDK没有jre,因此需要创建一个jre目录,再把jdk的lib文件夹复制到这个目录下,执行以下命令:
mkdir /usr/lib/jvm/jdk-10/jre && cp -r /usr/lib/jvm/jdk-10/lib /usr/lib/jvm/jdk-10/jre/
  1. 我用的是root账号,因此将OpenJDK11源码文件夹的所有者和用户组都改成root的,在目录/home/willzhao/work/compileopenjdk执行以下命令:
chown -R root jdk11 && chgrp -R root jdk11
  1. 安装必要的依赖应用:
apt-get install -y autoconf zip libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev libcups2-dev libfontconfig1-dev libasound2-dev
  1. 进入OpenJDK11源码的目录/home/willzhao/work/compileopenjdk/jdk11
  2. 配置确认,这一步通过后就可以开始编译了:
bash configure --with-num-cores=4 --with-memory-size=8192

以上命令中–with-num-cores=4表示四核CPU参与编译,–with-memory-size=8192表示8G内存参与编译,请您根据自己电脑的实际配置来调整;
如果配置不通过会有相关提示,请按照提示内容做调整,步骤3中的安装的那些应用其实都是配置检查不通过时提示要安装的;

  1. 配置确认通过后,控制台提示信息大致如下:
====================================================
A new configuration has been successfully created in
/home/willzhao/work/compileopenjdk/jdk11/build/linux-x86_64-normal-server-release
using configure arguments '--with-num-cores=4 --with-memory-size=8192'.

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-internal+0-adhoc.root.jdk11 (11-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 /usr/lib/jvm/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:   4
* Memory limit:   8192 MB
  1. 执行命令make即可开始编译,等待编译完成后控制台输出以下类似信息:
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'
  1. 打开jdk11/build/linux-x86_64-normal-server-release目录下的build.log文件可以看到更详细的日志:
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
----- Build times -------
Start 2018-10-23 14:45:20
End   2018-10-23 14:53:53

00:08:33 TOTAL
-------------------------
Finished building target 'default (exploded-image)' in configuration 'linux-x86_64-normal-server-release'
  1. 在jdk11/build/linux-x86_64-normal-server-release目录下,有个jdk目录,这里面就是最新构建的OpenJDK,进入里面的bin目录,再执行命令./java -version,可见最新的版本信息如下,已经是11版本了:
root@willzhao-Lenovo-Ubuntu16:/home/willzhao/work/compileopenjdk/jdk11/build/linux-x86_64-normal-server-release/jdk/bin# ./java -version
openjdk version "11-internal" 2018-09-25
OpenJDK Runtime Environment (build 11-internal+0-adhoc.root.jdk11)
OpenJDK 64-Bit Server VM (build 11-internal+0-adhoc.root.jdk11, mixed mode)

至此,基于OpenJDK11源码编译构建已经成功,去/etc/profile文件中做好环境变量设置就能正常使用新的JDK了;

欢迎关注我的公众号:程序员欣宸

在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐