编译JDK12

编译JDK12

前言

最近开始进行JVM的学习。在书中第一步便是编译JDK,参考书是《深入理解Java虚拟机》,并且以后的环境也需要。

刚开始我在ParrotOS系统中编译,出现挺多的问题,也不是什么大问题,一直因为卡的死死的而放弃。

本机只有8G内存想要全部利用,用虚拟机是不行的,于是便选择了Ubuntu的WSL,很棒,配置好环境后一次编译成功。选择WSL的原因还有一个,就是Clion可以直接连接使用。

环境及建议

建议先浏览一下官方文档的环境要求(我的虚拟机编译不成功很可能和内存太小有关)

仔细查看Build-Hardware-Requirements

配置环境

安装WSL

打开 Microsoft Store--> 搜索Ubuntu --> 安装Ubuntu 18.04 LTS

安装好后,建议更换为国内源,默认下载很慢

第一次使用WSL时可能需要使用管理员权限打开Powershell,运行下面的命令,然后重启:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

配置&安装环境依赖

基础需要

  • make
  • zip
  • unzip

安装CLang编译器

sudo apt install build-essential

安装OpenJDK编译依赖库

工具 库名称 实际
FreeType The FreeType Project libfreetype6-dev
CUPS Common UNIX Printing System libcups2-dev
X11 X Window System libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev
ALSA Advanced Linux Sound Architecture libasound2-dev
libffi Portable Foreign Function Interface libffi-dev
Autoconf Extensible Package of M4 Macros autoconf

可能在过程中会出现缺少库的问题,使用bash configure后会出现下面的例子,很人性化,根据句子中的提示安装所需的库即可

configure: error: Could not find alsa! You might be able to fix this by running 'sudo apt-get install libfontconfig1-dev'

安装JDK

假设要编译大版本号为N的JDK,我们需要安装版本号为N-1的JDK,因为部分代码是由C、C++编写,大部分还是由Java语言实现。我们要编译的是JDK12,所以需要JDK11来作“Bootstrap JDK”

sudo apt install openjdk-11-jdk

获取源码

hg clone http://hg.openjdk.java.net/jdk/jdk

下载很慢,建议在官网下载压缩包到本地。往往压缩包直接移动到WSL指定路径中,在WSL中ls命令是不显示的,应从/mnt/目录下找到对应盘符,然后找到文件并复制文件到指定位置。例如:

cp /mnt/f/jdk12.tar.gz /home/h31i
tar -xzvf jdk12.tar.gz

检查环境&配置

在目录下使用bash configure命令

要想带着调试、定制化的目的去编译,就要了解OpenJDK提供了编译参数,可以使用“bash configure --help”查看

cd jdk12
// 编译FastDebug版、仅含Server模式的HotSpot虚拟机
bash configure --enable-debug --with-jvm-variants=server

缺少依赖就安装。成功后会出现以下内容

A new configuration has been successfully created in
/home/h31i/jdk12/build/linux-x86_64-server-fastdebug
using configure arguments '--enable-debug --with-jvm-variants=server'.

Configuration summary:
* Debug level:    fastdebug
* HS debug level: fastdebug
* 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.h31i.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.4.0 (at /usr/bin/gcc)
* C++ Compiler:   Version 7.4.0 (at /usr/bin/g++)

Build performance summary:
* Cores to use:   4
* Memory limit:   8083 MB

/build/文件夹下会生成目标文件

编译&结果

执行make命令

make

然后,漫长的等待伴随沙沙的风扇声(预估30分钟左右)。

编译完成

Creating images/jmods/java.base.jmod
Creating support/demos/image/jfc/CodePointIM/CodePointIM.jar
Creating support/demos/image/jfc/FileChooserDemo/FileChooserDemo.jar
Creating support/demos/image/jfc/SwingSet2/SwingSet2.jar
Creating support/demos/image/jfc/Font2DTest/Font2DTest.jar
Creating support/demos/image/jfc/J2Ddemo/J2Ddemo.jar
Creating support/demos/image/jfc/Metalworks/Metalworks.jar
Creating support/demos/image/jfc/Notepad/Notepad.jar
Creating support/demos/image/jfc/Stylepad/Stylepad.jar
Creating support/demos/image/jfc/SampleTree/SampleTree.jar
Creating support/demos/image/jfc/TableExample/TableExample.jar
Creating support/demos/image/jfc/TransparentRuler/TransparentRuler.jar
Creating jdk image
Creating CDS archive for jdk image
Stopping sjavac server
Finished building target 'images' in configuration 'linux-x86_64-server-fastdebug'

测试

生成的JDK在/build/linux-x86_64-server-fastdebug/中,测试一下,如:

h31i@hostname:~/jdk12/build/linux-x86_64-server-fastdebug/jdk/bin$ ./java -version
openjdk version "12-internal" 2019-03-19
OpenJDK Runtime Environment (fastdebug build 12-internal+0-adhoc.h31i.jdk12)
OpenJDK 64-Bit Server VM (fastdebug build 12-internal+0-adhoc.h31i.jdk12, mixed mode)

错误

如果在编译过程中出现问题,一句句代码的错误,请在bash configure时加上--disable-warnings-as-errors参数

重新编译时建议先make clean

强迫症(比如我)可以去/build/文件下删除指定文件

下一步就是连接CLion了,再见

References:

CLion连接WSL

posted on 2019-12-27 12:47  Kn0w_N0ThInG  阅读(918)  评论(0编辑  收藏  举报

导航