openjdk 1.8 源码编译

1,623 阅读4分钟

编译环境

环境说明

Ubuntu 20.04.1 LTS

OpenJDK jdk8-b120

注意:Ubuntu 配置 apt 安装源为阿里源可以提高加载速度

设置 设置 apt 安装阿里源

  1. 设置 apt 安装阿里源
sudo cp /ect/apt/sources.list /etc/apt/sources.list.bak
  1. 添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
  1. 更新源
sudo apt-get update
sudo apt-get upgrade

下载 jdk 源码到本地

官网地址:openjdk.java.net/
github 源码地址: github.com/openjdk/
我的下载地址: gitee.com/zhengsh/jdk

下载命令:

# 由于github 国内网络太慢我已经将 openjdk 代码同步到 gitee
~/javawork$ git clone https://gitee.com/zhengsh/jdk.git

执行编译

  1. 安装 open-jdk-8

    sudo apt-get install openjdk-8-jdk
    
  2. 编译代码

    # 执行编译
    bash ./configure
    
  3. 错误提示:configure: error: Cannot find GNU make 3.81 or newer! Please put it in the path, or add e.g. MAKE=/opt/gmake3.81/make as argument to configure.

    # 我的是 make 没有安装
    sudo apt-get install make
    
    #如果其它提示可以参考 java bug  https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8027567
    
  4. 错误提示:configure: error: Could not find a C compiler. You might be able to fix this by running 'sudo apt-get install build-essential'.

    # 安装 build-essential
    sudo apt-get install build-essential
    
  5. 错误提示:configure: error: Could not find all X11 headers (shape.h Xrender.h XTest.h Intrinsic.h). You might be able to fix this by running 'sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev'.

    # 安装 libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
    # 注意 libx11-dev 的 X 是小写
    sudo apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
    
  6. 错误提示:configure: error: Could not find cups! You might be able to fix this by running 'sudo apt-get install libcups2-dev'.

    # 安装 libcups2-dev
    sudo apt-get install libcups2-dev
    
  7. 错误提示:configure: error: Could not find freetype! You might be able to fix this by running 'sudo apt-get install libfreetype6-dev'.

    # 安装 libfreetype6-dev
    sudo apt-get install libfreetype6-dev
    
  8. 错误提示:configure: error: Could not find alsa! You might be able to fix this by running 'sudo apt-get install libasound2-dev'.

    # 安装 libasound2-dev
    sudo apt-get install libasound2-dev
    
  9. 成功提示

    Tools summary:
    * Boot JDK:       openjdk version "1.8.0_275" OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01) OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)  (at /usr/lib/jvm/java-8-openjdk-amd64)
    * C Compiler:     x86_64-linux-gnu-gcc-9 (Ubuntu 9.3.0-17ubuntu1~20.04) version 9.3.0 (at /usr/bin/x86_64-linux-gnu-gcc-9)
    * C++ Compiler:   x86_64-linux-gnu-g++-9 (Ubuntu 9.3.0-17ubuntu1~20.04) version 9.3.0 (at /usr/bin/x86_64-linux-gnu-g++-9)
    
    Build performance summary:
    * Cores to use:   1
    * Memory limit:   3936 MB
    * ccache status:  not installed (consider installing)
    
    Build performance tip: ccache gives a tremendous speedup for C++ recompilations.
    You do not have ccache installed. Try installing it.
    You might be able to fix this by running 'sudo apt-get install ccache'.
    
  10. make all

    # 错误提示,(解决方法降低 gcc 版本)
    make[6]: *** [/home/zsh/javawork/jdk/hotspot/make/linux/makefiles/vm.make:297: precompiled.hpp.gch] Error 1
    make[5]: *** [/home/zsh/javawork/jdk/hotspot/make/linux/makefiles/top.make:119: the_vm] Error 2
    make[4]: *** [/home/zsh/javawork/jdk/hotspot/make/linux/Makefile:289: product] Error 2
    make[3]: *** [Makefile:217: generic_build2] Error 2
    make[2]: *** [Makefile:167: product] Error 2
    make[1]: *** [HotspotWrapper.gmk:45: /home/zsh/javawork/jdk/build/linux-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
    make: *** [/home/zsh/javawork/jdk//make/Main.gmk:109: hotspot-only] Error 2
    
    # gcc --version
    gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    # 安装 gcc-4.8 g++-4.8
    sudo apt-get install -y gcc-4.8
    sudo apt-get install -y g++-4.8
    
    # 重新建立软连接
    cd /usr/bin             #进入/usr/bin文件夹下
    sudo rm -r gcc          #移除之前的软连接
    sudo ln -sf gcc-4.8 gcc #建立gcc4.8的软连接
    sudo rm -r g++
    sudo ln -sf g++-4.8 g++
    
    # 重新查询 gcc 版本
    gcc --version 
    
    gcc (Ubuntu 4.8.5-4ubuntu8) 4.8.5
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    

    make all 成功提示信息

    ----- Build times -------
    Start 2020-12-12 22:17:38
    End   2020-12-12 22:51:31
    00:00:54 corba
    00:01:03 demos
    00:04:57 docs
    00:16:08 hotspot
    00:01:06 images
    00:00:35 jaxp
    00:01:05 jaxws
    00:06:49 jdk
    00:00:54 langtools
    00:00:21 nashorn
    00:33:53 TOTAL
    -------------------------
    Finished building OpenJDK for target 'all'
    
    

编译成功

    cd /javawork/jdk/build/linux-x86_64-normal-server-release/jdk/bin
    ./java -version

    # 到此 openjdk 编译成功
    openjdk version "1.8.0-internal"
    OpenJDK Runtime Environment (build 1.8.0-internal-zsh_2020_12_12_22_17-b00)
    OpenJDK 64-Bit Server VM (build 25.0-b62, mixed mode)

总结

  1. 其实我没有看特别教程, 就参考了,OpenJdk 中的 README 文件,然后结合提示解决问题 Google 解决问题。
  2. 系统要求,建议虚拟机在 4核心8G 内存不然过程比较漫长。
  3. 这些都是环境搭建层面上的东西,后续我将继续完善 openjdk 相关的笔记