In Ubuntu18.04 compile OpenJDK12

table of Contents

1, the required software

1.1 OpenJDK12

1.2 Ubuntu 18.04 LTS

1.3 Workstation 14 Pro(14.1.0 build-7370693)

2, building build environment

Construction of GCC 2.1 compiler environment

2.2 OpenJDK compiled dependent libraries

2.3 OpenJDK11

2.4 remind

3, compile

3.1 dependency checking

3.2 compiler

4, reference


1, the required software

1.1 OpenJDK12

Direct access to the JDK version of the warehouse ready to download page (https://hg.openjdk.java.net/jdk/jdk12/), then click on the left menu "Browse", will show the source code root directory page in the following figure. Then click on the left of the "zip" link to download the current version packaged source code, to be directly extracted locally. In OpenJDK 12 Ubuntu download source packet size of 180.3MB, after decompression about 675.6MB.

1.2 Ubuntu 18.04 LTS

URL: https://ubuntu.com/download/desktop

1.3 Workstation 14 Pro(14.1.0 build-7370693)

2, building build environment

Construction of GCC 2.1 compiler environment

Ubuntu where the user can choose to install or CLang GCC to compile, but must ensure that the minimum version of GCC 4.8 CLang 3.2 or above, the official recommended to use GCC 7.8 or CLang 9.1 to compile. GCC command to install on Ubuntu systems:

sudo apt-get install build-essential

The test gcc version installed: gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1 ~ 18.04) 

2.2 OpenJDK compiled dependent libraries

During compilation rely FreeType, CUPS several other third-party libraries, the OpenJDK all dependencies are listed in the following table, the corresponding executable installation command to complete the installation.

tool Library name Installation Commands
FreeType The Free Type Project sudo apt-get install libfreetype6-dev
CUPS Common UNIX Printing System sudo apt-get install libcups2-dev
X11 X Window System

sudo apt-get install libx11-dev libxext-dev libxrender-

dev libxrandr-dev libxtst-dev libxt-dev

ALSA Advanced Linux Sound Architecture sudo apt-get install libasound2-dev
libffi Portable Foreign Function Interface Library sudo apt-get install libffi-dev
Autoconf Extensible Package of M4 Macros sudo apt-get install autoconf

2.3 OpenJDK11

假设要编译大版本号为N的JDK,我们还要另外准备一个大版本号至少为N-1的、已经编译好的JDK,这是因为OpenJDK由多个部分(HotSpot、JDK类库、JAXWS、JAXP……)构成,其中一部分(HotSpot)代码使用C、C++编写,而更多的代码则是使用Java语言来实现,因此编译这些Java代码就需要用到另一个编译期可用的JDK,官方称这个JDK为“Bootstrap JDK”。编译OpenJDK 12时,Bootstrap JDK必须使用JDK 11及之后的版本。在Ubuntu中使用以下命令安装OpenJDK 11:

sudo apt-get install openjdk-11-jdk

2.4 提醒

对系统环境的最后一点建议是,所有的文件,包括源码和依赖项目,都不要放在包含中文的目录里面,这样做不是一定会产生不可解决的问题,只是没有必要给自己找麻烦。

3、进行编译

需要下载的编译环境和依赖项目都齐备后,我们就可以按照默认配置来开始编译了。

3.1 依赖检查

configure命令承担了依赖项检查、参数配置和构建输出目录结构等多项职责,如果编译过程中需要的工具链或者依赖项有缺失,命令执行后将会得到明确的提示,并且给出该依赖的安装命令,这比编译旧版OpenJDK时的“make sanity”检查要友好得多。

出现错误

checking for fontconfig/fontconfig.h... no
configure: error: Could not find fontconfig! You might be able to fix this by running 'sudo apt-get install libfontconfig1-dev'. 
configure exiting with result code 1

按照错误提示,执行:

sudo apt-get install libfontconfig1-dev

然后再次执行:sudo bash configure,结果如下:

//省略

====================================================
A new configuration has been successfully created in
/home/develop/jdk12/build/linux-x86_64-server-release
using default settings.

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 shenandoahgc vm-structs zgc' 
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 12-internal+0-adhoc.root.jdk12 (12-internal)

Tools summary:
* Boot JDK:       openjdk version "11.0.5" 2019-10-15 OpenJDK Runtime Environment (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04) OpenJDK 64-Bit Server VM (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04, mixed mode, sharing)  (at /usr/lib/jvm/java-11-openjdk-amd64)
* Toolchain:      gcc (GNU Compiler Collection)
* C Compiler:     Version 7.5.0 (at /usr/bin/gcc)
* C++ Compiler:   Version 7.5.0 (at /usr/bin/g++)

Build performance summary:
* Cores to use:   2
* Memory limit:   7950 MB

openjdk12@ubuntu:/home/develop/jdk12$ 

以上表明:配置成功,输出调试级别,Java虚拟机的模式、特性,使用的编译器版本等配置摘要信息。

3.2 编译

openjdk12@ubuntu:/home/develop/jdk12$ sudo make images

依赖检查通过后便可以输入“make images”执行整个OpenJDK编译了,这里“images”是“productimages”编译目标(Target)的简写别名,这个目标的作用是编译出整个JDK镜像,除了“productimages”以外,其他编译目标还有:

hotspot:只编译HotSpot虚拟机
hotspot-<variant>:只编译特定模式的HotSpot虚拟机
docs-image:产生JDK的文档镜像
test-image:产生JDK的测试镜像
all-image:相当于连续调用product、docs、test三个编译目标
bootcycle-images:编译两次JDK,其中第二次使用第一次的编译结果作为Bootstrap JDK
clean:清理make命令产生的临时文件
dist-clean:清理make和configure命令产生的临时文件

结果

编译完成后,进入OpenJDK源码的“build/配置名称/jdk”目录下就可以看到OpenJDK的完整编译结果:

将它复制到JAVA_HOME目录,就可以作为一个完整的JDK来使用,若没有设置过JDK开发版本的话,这个JDK的开发版本号会默认带上编译的机器名。

为了方便,我直接将编译完成的jdk的路径设置为JAVA_HOME。执行“sudo gedit ~/.bashrc”,在最后添加:

export JAVA_HOME=/home/develop/jdk12/build/linux-x86_64-server-release/jdk
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

保存后执行“source ~/.bashrc”使得修改生效,使用“java -version”检查配置是否生效。

到此为止,已经编译好了一个HotSpot虚拟机。

用Hello测试一下:

openjdk12@ubuntu:/test$ ll
total 12
drwxr-xr-x  2 root root 4096 Jan 26 01:02 ./
drwxr-xr-x 25 root root 4096 Jan 26 00:58 ../
-rw-r--r--  1 root root  116 Jan 26 01:02 Hello.java
openjdk12@ubuntu:/test$ sudo javac Hello.java 
openjdk12@ubuntu:/test$ sudo java Hello
Hello Wordl!

下面是虚拟机的信息。 

4、参考

《深入理解Java虚拟机:JVM高级特性与最佳实践》(第三版)

发布了95 篇原创文章 · 获赞 16 · 访问量 5万+

Guess you like

Origin blog.csdn.net/tiankong_12345/article/details/104086288