在Ubuntu20下编译OpenJDK8

编译OpenJDK8

最近看了《深入理解Java虚拟机》这本书,想动手编译自己的JDK,按照书里的说法开始想编译OpenJDK7来着,但是折腾了半天,最后成功了之后测试时一直在提示 Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again,但是执行了提示的命令也不好使,水平有限就不深究了,想着能不能换成8的版本。尝试了一下,在解决完过程的问题之后,竟然成功了,在此记录,希望能对同样在折腾的小伙伴有一些帮助。

本文是对自己的操作过程做的记录,经历比较坎坷。

环境准备

操作系统还是原来的操作系统,不多说,用的ubuntu 20.04,系统信息如下。
在这里插入图片描述

下载 OpenJDK 源代码

访问 源代码下载地址 下载源代码

wget https://download.java.net/openjdk/jdk8u41/ri/openjdk-8u41-src-b04-14_jan_2020.zip

查看源代码

首先,先来查看一下源代码,目录如下:

➜  openjdk ll
总用量 234K
-rw-r--r--  1 zhanggy zhanggy 1.5K 1月  15  2020 ASSEMBLY_EXCEPTION
drwxr-xr-x  6 zhanggy zhanggy    6 1月  15  2020 common
-rw-r--r--  1 zhanggy zhanggy 1.3K 1月  15  2020 configure
drwxr-xr-x  5 zhanggy zhanggy   11 1月  15  2020 corba
-rw-r--r--  1 zhanggy zhanggy 3.1K 1月  15  2020 get_source.sh
drwxr-xr-x  7 zhanggy zhanggy   13 1月  15  2020 hotspot
drwxr-xr-x  6 zhanggy zhanggy   15 1月  15  2020 jaxp
drwxr-xr-x  6 zhanggy zhanggy   15 1月  15  2020 jaxws
drwxr-xr-x  6 zhanggy zhanggy   13 1月  15  2020 jdk
drwxr-xr-x  6 zhanggy zhanggy   12 1月  15  2020 langtools
-rw-r--r--  1 zhanggy zhanggy  19K 1月  15  2020 LICENSE
drwxr-xr-x  6 zhanggy zhanggy   12 1月  15  2020 make
-rw-r--r--  1 zhanggy zhanggy 6.1K 1月  15  2020 Makefile
drwxr-xr-x 12 zhanggy zhanggy   19 1月  15  2020 nashorn
-rw-r--r--  1 zhanggy zhanggy 1.6K 1月  15  2020 README
-rw-r--r--  1 zhanggy zhanggy 127K 1月  15  2020 README-builds.html
drwxr-xr-x  2 zhanggy zhanggy    3 1月  15  2020 test
-rw-r--r--  1 zhanggy zhanggy 175K 1月  15  2020 THIRD_PARTY_README

查看系统缺失的依赖

OpenJDK 的./configure 自带依赖的检查,可以查看编译的必要依赖是否存在。

# 先将 configure 授执行的权限
chmod u+x ./configure

# 执行 configure
➜  openjdk chmod u+x configure 
➜  openjdk ./configure 
Running generated-configure.sh
configure: Configuration created at Wed Jan 20 21:47:01 CST 2021.
configure: configure script generated at timestamp 1573688723.
checking for basename... /usr/bin/basename
... # 省略中间输出
configure: Found potential Boot JDK using well-known locations (in /usr/lib/jvm/java-11-openjdk-amd64)
configure: Potential Boot JDK found at /usr/lib/jvm/java-11-openjdk-amd64 did not contain bin/javac; ignoring
configure: (This might be an JRE instead of an JDK)
configure: Found potential Boot JDK using well-known locations (in /usr/lib/jvm/java-1.11.0-openjdk-amd64)
configure: Potential Boot JDK found at /usr/lib/jvm/java-1.11.0-openjdk-amd64 did not contain bin/javac; ignoring
configure: (This might be an JRE instead of an JDK)
configure: Found potential Boot JDK using well-known locations (in /usr/lib/jvm/default-java)
configure: Potential Boot JDK found at /usr/lib/jvm/default-java did not contain bin/javac; ignoring
configure: (This might be an JRE instead of an JDK)
configure: Could not find a valid Boot JDK. You might be able to fix this by running 'sudo apt-get install openjdk-7-jdk'.
configure: This might be fixed by explicitely setting --with-boot-jdk
configure: error: Cannot continue
configure exiting with result code 1

检核结果提示缺少 Boot JDK
因为在当前的源中已经移除了openjdk-7-jdk包,所以无法用建议的命令补全依赖。
此时下载Oracle JDK7作为Boot JDK。至于为什么采用jdk7作为Boot JDK 而不是其他版本,后续再讲。配置 Boot JDK 的环境变量与正常安装JDK的环境变量一般无二通常是配置JAVA_HOMEPATH

再次检查依赖

➜  openjdk ./configure 
Running generated-configure.sh
configure: Configuration created at Wed Jan 20 21:56:55 CST 2021.
configure: configure script generated at timestamp 1573688723.
... # 省略中间输出
checking for jtreg... no
checking for gcc... no
checking for cc... no
configure: error: Could not find a C compiler. You might be able to fix this by running 'sudo apt-get install build-essential'.
configure exiting with result code 1

Could not find a C compiler. 没有找到C语言的编译器,按照系统的提示,我们安装一个C编译器sudo apt-get install build-essential,同时也会安装本身所依赖的包,等待安装完成…

三次检查依赖

后续还会检查出一堆编译需要的依赖包,根据提示用相同的方式安装依赖包,这里不再赘述,

➜  openjdk sudo apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
➜  openjdk sudo apt-get install libcups2-dev
➜  openjdk sudo apt-get install libasound2-dev

又出现一个错误

Build performance tip: ccache gives a tremendous speedup for C++ recompilations.
You have ccache installed, but it is a version prior to 3.1.4. Try upgrading.
You might be able to fix this by running 'sudo apt-get install ccache'.

此错误按照提示并不能修复。本身ccache已经安装了,只不过版本比较高

不过可以禁用,使用如下的配置

➜  openjdk ./configure --disable-ccache

输出下列字样,代表检核通过了

====================================================
A new configuration has been successfully created in
/home/zhanggy/Downloads/openjdk/build/linux-x86_64-normal-server-release
using configure arguments '--disable-ccache'.

Configuration summary:
* Debug level:    release
* JDK variant:    normal
* JVM variants:   server
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64

Tools summary:
* Boot JDK:       java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)  (at /home/zhanggy/Downloads/jdk1.7.0_80)
* 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:   3
* Memory limit:   3848 MB
* ccache status:  not installed (consider installing)

WARNING: The result of this configuration has overridden an older
configuration. You *should* run 'make clean' to make sure you get a
proper build. Failure to do so might result in strange build problems.

此时还有一个警告,不过作为 “devor”,类似于“吸烟有害健康”之类的警告基本上都是忽略…

开始编译

➜  openjdk make

又报错了

INFO: ZIP_DEBUGINFO_FILES=1
*** This OS is not supported: Linux zgy-ubuntu 5.8.0-38-generic #43~20.04.1-Ubuntu SMP Tue Jan 12 16:39:47 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
make[5]: *** [/home/zhanggy/Downloads/openjdk/hotspot/make/linux/Makefile:238:check_os_version] 错误 1
make[4]: *** [/home/zhanggy/Downloads/openjdk/hotspot/make/linux/Makefile:259:linux_amd64_compiler2/debug] 错误 2
make[3]: *** [Makefile:231:generic_build2] 错误 2
make[2]: *** [Makefile:177:product] 错误 2
make[1]: *** [HotspotWrapper.gmk:45:/home/zhanggy/Downloads/openjdk/build/linux-x86_64-normal-server-release/hotspot/_hotspot.timestamp] 错误 2
make: *** [/home/zhanggy/Downloads/openjdk//make/Main.gmk:109:hotspot-only] 错误 2

根据提示,查看源代码

SUPPORTED_OS_VERSION = 2.4% 2.5% 2.6% 3%
OS_VERSION := $(shell uname -r)
EMPTY_IF_NOT_SUPPORTED = $(filter $(SUPPORTED_OS_VERSION),$(OS_VERSION))

check_os_version:
ifeq ($(DISABLE_HOTSPOT_OS_VERSION_CHECK)$(EMPTY_IF_NOT_SUPPORTED),)
        $(QUIETLY) >&2 echo "*** This OS is not supported:" `uname -a`; exit 1;
endif

当前系统的内核

➜  openjdk uname -r
5.8.0-38-generic

将 SUPPORTED_OS_VERSION 添加当前内核的支持

SUPPORTED_OS_VERSION = 2.4% 2.5% 2.6% 3% 4% 5%
OS_VERSION := $(shell uname -r)
EMPTY_IF_NOT_SUPPORTED = $(filter $(SUPPORTED_OS_VERSION),$(OS_VERSION))

check_os_version:
ifeq ($(DISABLE_HOTSPOT_OS_VERSION_CHECK)$(EMPTY_IF_NOT_SUPPORTED),)
        $(QUIETLY) >&2 echo "*** This OS is not supported:" `uname -a`; exit 1;
endif

再次编译…

错误太多,不一一展示,多是一些 C++11之类的错误
可以看出来,c++11或者说高版本的c++语法和低版本的c++语法在某此地方不兼容,sudo apt-get install 安装的 gccg++的版本太高了,与源代码编译所需的版本不同,所以有许多语法上的错误。

处理方式:需要将高版的g++替换为低版的g++

当前源中有的最低版本的g++和gcc是7的版本,尝试一下。

➜  Download sudo apt-get install g++-7
...
# 将/usr/bin 下的gcc和g++以及相关的工具都换成 7 的版本,删除原来的软链接,重新创建
➜  Download cd /usr/bin
➜  bin sudo rm g++ && sudo ln -s g++-7 g++
➜  bin sudo rm gcc && sudo ln -s gcc-7 gcc
➜  bin sudo rm gcc-ar && sudo ln -s gcc-ar-7 gcc-ar
➜  bin sudo rm gcc-nm && sudo ln -s gcc-nm-7 gcc-nm
➜  bin sudo rm gcc-ranlib && sudo ln -s gcc-ranlib-7 gcc-ranlib
➜  bin sudo rm gcov && sudo ln -s gcov-7 gcov 
➜  bin sudo rm gcov-dump && sudo ln -s gcov-dump-7 gcov-dump 
➜  bin sudo rm gcov-tool && sudo ln -s gcov-tool-7 gcov-tool

重新执行

➜  openjdk ./configure --disable-ccache
➜  openjdk make

依旧报错,还是C++11之类的错误,版本还是太高。

能否换更低的版本?百度网上的资料,大多数编译成功的案例还是用的g++ 4.*的版本,添加一个包含g++ 4.*的源,安装g+±4.9。

➜  openjdk sudo vim /etc/apt/sources.list
# 添加两行数据到最后:
# # for gcc 4.* g++ 4.*
# deb http://dk.archive.ubuntu.com/ubuntu/ xenial main
# deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe

➜  openjdk sudo apt update
➜  openjdk sudo apt-get install g++-4.9
... # 会自动安装依赖的 gcc-4.9

➜  openjdk cd /usr/bin
➜  bin sudo rm g++ && sudo ln -s g++-4.9 g++
➜  bin sudo rm gcc && sudo ln -s gcc-4.9 gcc 
➜  bin sudo rm gcc-ar && sudo ln -s gcc-ar-4.9 gcc-ar 
➜  bin sudo rm gcc-nm && sudo ln -s gcc-nm-4.9 gcc-nm 
➜  bin sudo rm gcc-ranlib && sudo ln -s gcc-ranlib-4.9 gcc-ranlib 
➜  bin sudo rm gcov && sudo ln -s gcov-4.9 gcov  

果然 C++11的错误没有了,还剩下下面的错误:

/home/zhanggy/Downloads/openjdk/hotspot/src/os/linux/vm/os_linux.inline.hpp: In static member function ‘static dirent* os::readdir(DIR*, dirent*)’:
/home/zhanggy/Downloads/openjdk/hotspot/src/os/linux/vm/os_linux.inline.hpp:127:18: error: ‘int readdir_r(DIR*, dirent*, dirent**)’ is deprecated (declared at /usr/include/dirent.h:183) [-Werror=deprecated-declarations]
   if((status = ::readdir_r(dirp, dbuf, &p)) != 0) {
                  ^
/home/zhanggy/Downloads/openjdk/hotspot/src/os/linux/vm/os_linux.inline.hpp:127:42: error: ‘int readdir_r(DIR*, dirent*, dirent**)’ is deprecated (declared at /usr/include/dirent.h:183) [-Werror=deprecated-declarations]
   if((status = ::readdir_r(dirp, dbuf, &p)) != 0) {
                                          ^
cc1plus: all warnings being treated as errors
make[6]: *** [/home/zhanggy/Downloads/openjdk/hotspot/make/linux/makefiles/vm.make:304:precompiled.hpp.gch] 错误 1
make[5]: *** [/home/zhanggy/Downloads/openjdk/hotspot/make/linux/makefiles/top.make:119:the_vm] 错误 2
make[4]: *** [/home/zhanggy/Downloads/openjdk/hotspot/make/linux/Makefile:293:product] 错误 2
make[3]: *** [Makefile:231:generic_build2] 错误 2
make[2]: *** [Makefile:177:product] 错误 2
make[1]: *** [HotspotWrapper.gmk:45:/home/zhanggy/Downloads/openjdk/build/linux-x86_64-normal-server-release/hotspot/_hotspot.timestamp] 错误 2
make: *** [/home/zhanggy/Downloads/openjdk//make/Main.gmk:109:hotspot-only] 错误 2

以自己还在好些年前学习的C++的经验看确实没有看出来哪里有问题,百度搜索得知因为glibc版本大于等于2.24的情况下,方法 readdir_r被弃用了,所以在调用的时候会出现error的提示,本地版本如下,比较高。

➜  openjdk getconf GNU_LIBC_VERSION
glibc 2.31

注释该段或修改源代码,再编译就会忽略掉警告,直到编译完成。。

➜  openjdk vim ./hotspot/make/linux/makefiles/gcc.make
# 修改源代码如下:
208 # WARNINGS_ARE_ERRORS = -Werror
209 WARNINGS_ARE_ERRORS = -Wno-all

继续编译…

编译成功

编译成功后会输出如下的字样。

...  #省略前面一堆日志输出
Creating jdk/jce/unsigned/sunpkcs11.jar
Creating jdk/jce/unsigned/sunec.jar
Creating jdk/jce/unsigned/sunjce_provider.jar
Creating jdk/jce/unsigned/jce.jar
Creating jdk/jce/unsigned/policy/unlimited/US_export_policy.jar
Creating jdk/jce/unsigned/policy/limited/local_policy.jar
Creating jdk/jce/unsigned/policy/unlimited/local_policy.jar
## Finished jdk (build time 00:09:13)

----- Build times -------
Start 2021-01-21 00:51:39
End   2021-01-21 01:31:32
00:01:21 corba
00:27:19 hotspot
00:00:52 jaxp
00:01:08 jaxws
00:09:13 jdk
00:00:00 langtools
00:39:53 TOTAL
-------------------------
Finished building OpenJDK for target 'default'

试一下java命令

➜  bin pwd
/home/zhanggy/Downloads/openjdk/build/linux-x86_64-normal-server-release/jdk/bin
➜  bin ./java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-zhanggy_2021_01_21_00_29-b00)
OpenJDK 64-Bit Server VM (build 25.40-b25, mixed mode)

至此,历时一天,编译过程耗时40分钟的 OpenJDK8 编译成功。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值