编译OpenJDK12实战

跟着深入理解jvm3一书中实战操作,自编译JDK12

下载源码

直接下载zip(不推荐)

原书从官网源码下载(不推荐

GItHub仓库(推荐)

推荐直接GitHub拉(最近clone的速度快了挺多),大概两三分钟就clone完毕。直接clone到的是JDK最新版本,详解看 进行编译 第2步。

1
git clone https://github.com/openjdk/jdk

系统

我用的和书中一样的ubuntu18,我的是Ubuntu 18.04.6 LTS x86_64

构建编译环境

  1. 依赖

    1
    sudo apt-get install build-essential
  2. 安装OpenJDK 11。要编译大版本为N的JDK,要另外准备一个大版本号至少为N-1的、已编译好的JDK。

    1
    sudo apt-get install openjdk-11-jdk

进行编译

  1. 获取编译参数帮助

    1
    bash configure --help

    我这里因为没有安装autoconf会报一个错误Error: Cannot find autoconf,遂安装后再执行获取help即可

    1
    sudo apt-get install autoconf
  2. 配置

    编译FastDebug版、仅含Server模式的HotSpot虚拟机

    1
    bash configure --enable-debug --with-jvm-variants=server

    在我的ubuntu环境执行配置命令,接收到以下错误

    1
    2
    3
    4
    5
    configure: (Your Boot JDK version must be one of: 18 19)
    configure: Could not find a valid Boot JDK. OpenJDK distributions are available at http://jdk.java.net/.
    configure: This might be fixed by explicitly setting --with-boot-jdk
    configure: error: Cannot continue
    configure exiting with result code 1

    很明显的提示了前置的JDK版本不对,因为之前安装的是JDK11,要求18或19。由于我直接从GitHub上拉的最新仓库的源码版本为JDK19的,所以需要替换为JDK12的版本。从仓库的Tags找到JDK12的源码,下载解压后,进入该解压目录从新执行

    1
    bash configure --enable-debug --with-jvm-variants=server

    缺少一些依赖库,报错,按照提示一个个安装,再重新跑一次配置命令即可

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #configure: error: Could not find all X11 headers (shape.h Xrender.h Xrander.h XTest.h Intrinsic.h). You might be able to fix this by running 'sudo apt-get install libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev'.
    #configure exiting with result code 1
    sudo apt-get install libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev
    #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
    #configure: error: Could not find alsa! You might be able to fix this by running 'sudo apt-get install libasound2-dev'.
    #configure exiting with result code 1
    sudo apt-get install libasound2-dev

    输出成功后输出:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    ====================================================
    A new configuration has been successfully created in
    /home/k/Downloads/jdk-jdk-12-27/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.k.jdk-jdk-12-27 (12-internal)

    Tools summary:
    * Boot JDK: openjdk version "11.0.14.1" 2022-02-08 OpenJDK Runtime Environment (build 11.0.14.1+1-Ubuntu-0ubuntu1.18.04) OpenJDK 64-Bit Server VM (build 11.0.14.1+1-Ubuntu-0ubuntu1.18.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: 3
    * Memory limit: 3919 MB

    在configure命令以及后面的make命令的执行过程中,会在“build/配置名称”目录下产生如下目录结
    构。不常使用C/C++的读者要特别注意,如果多次编译,或者目录结构成功产生后又再次修改了配
    置,必须先使用“make clean”和“make dist-clean”命令清理目录,才能确保新的配置生效。编译产生的目
    录结构以及用途如下所示:

  3. make images

    前排提示,执行该命令之前分配多点内存和核心数(虚拟机下的话)

    1
    make images

    等待…

    出错:内存不足(分配了4G),由于是虚拟机,swap的空间也挺小1.4G。

    1
    2
    3
    4
    5
    6
    7
    OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000ae800000, 547356672, 0) failed; error='Not enough space' (errno=12)
    #
    # There is insufficient memory for the Java Runtime Environment to continue.
    # Native memory allocation (mmap) failed to map 547356672 bytes for committing reserved memory.
    # An error report file with more information is saved as:
    # /home/k/Downloads/jdk-jdk-12-27/make/gensrc/hs_err_pid76398.log

    可以使用命令查看内存和swap大小

    1
    free -m

    我给虚拟机分配了8G内存,重新编译得出如下:

    1
    2
    Stopping sjavac server
    Finished building target 'images' in configuration 'linux-x86_64-server-fastdebug'

    进入提示的路径./build/linux-x86_64-server-fastdebug/jdk/bin,执行version命令

    1
    ./java -version

    输出

    1
    2
    3
    openjdk version "12-internal" 2019-03-19
    OpenJDK Runtime Environment (fastdebug build 12-internal+0-adhoc.k.jdk-jdk-12-27)
    OpenJDK 64-Bit Server VM (fastdebug build 12-internal+0-adhoc.k.jdk-jdk-12-27, mixed mode)

    编译完成。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!