天天看点

编译自己的OpenJDK

0:官方文档

http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html

上面给出了编译OpenJDK的官方文档,可以自行阅读一遍,了解详情

1:环境准备

最好在linux、mac上面进行操作,相比windows会简单很多

linux可以通过执行下面的命令得到系统版本信息

lsb_release  -a
           

下面是我linux的版本信息我是centos的,官方是在ubuntu上面进行操作的,最好跟官方一致,可以少踩一些坑

LSB Version:	:core-4.1-amd64:core-4.1-noarch
Distributor ID:	CentOS
Description:	CentOS Linux release 7.4.1708 (Core)
Release:	7.4.1708
Codename:	Core
           

2:获取源码

执行下面的这命令

hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
           

如果出现下面的信息,就表示还没有安装mercurial

[email protected]:/opt/open-compiler# hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
The program 'hg' is currently not installed.  You can install it by typing:
apt-get install mercurial
           

执行下面的这行代码安装mercurial,如果没有yum,自行安装yum

yum  install mercurial
           

安装完成后,接着执行下面的这行代码,获取源码

hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
           

如果出现下面的这些提示表示下载完成(部分同学有可能出现英文形式的下面的这些提示吗,也是一样的)

正在请求全部修改
正在增加修改集
正在增加清单
正在增加文件改变
已增加 942 个修改集,包含 1309 个改变,修改了 136 个文件
updating to branch default
82 files updated, 0 files merged, 0 files removed, 0 files unresolved
           

如果出现下面的这行提示,表示下载源码的过程中出现了一些意外,重新执行下载的那个命令,直到将源代码下载下来就好了

正在请求全部修改
正在增加修改集
正在增加清单
正在增加文件改变
事务中止!
完成回滚
中止: stream ended unexpectedly (got 9444 bytes, expected 24147)
           

3:确认编译环境是否准备完全

执行下面的这行命令

bash ./configure
           

如果确认编译环境的时候出现异常,那么就安装需要的环境信息,例如出现下面的这个错误,这个就表示没有unzip,那么就安装unzip

checking for unzip... no
configure: Could not find unzip!
configure: error: Cannot continue
configure exiting with result code 1
           

部分确实的环境他会提供安装的命令,按照提示进行安装就好了,例如下面的这个

checking for javac... no
checking for java... no
configure: Could not find a valid Boot JDK. You might be able to fix this by running 'sudo yum install java-1.7.0-openjdk'.
configure: This might be fixed by explicitely setting --with-boot-jdk
configure: error: Cannot continue
configure exiting with result code 1
           

但是按照他给的提示进行安装之后,重新执行bash ./configure 命令之后又出现了下面的这个错误

checking for java... /usr/bin/java
configure: Found potential Boot JDK using well-known locations (in /usr/lib/jvm/jre-openjdk)
configure: Potential Boot JDK found at /usr/lib/jvm/jre-openjdk 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/jre-1.7.0-openjdk.x86_64)
configure: Potential Boot JDK found at /usr/lib/jvm/jre-1.7.0-openjdk.x86_64 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/jre-1.7.0)
configure: Potential Boot JDK found at /usr/lib/jvm/jre-1.7.0 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/jre)
configure: Potential Boot JDK found at /usr/lib/jvm/jre 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.7.0-openjdk-1.7.0.45.x86_64)
configure: Potential Boot JDK found at /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64 did not contain bin/java; ignoring
configure: Could not find a valid Boot JDK. You might be able to fix this by running 'sudo yum install java-1.7.0-openjdk'.
configure: This might be fixed by explicitely setting --with-boot-jdk
configure: error: Cannot continue
configure exiting with result code 1
           

出现这个问题应该是你按照他安装的只是安装了jre,而没有安装jdk(我猜的),然后我去网上找了下安装openJdk的,就是执行下面的这个命令,执行完毕后再执行 bash ./configure命令就能够通过jdk的这个环境了,安装之后会在使用这个路径下面的Boot JDK:/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.201-2.6.16.1.el7_6.x86_64

yum install java-1.7.0-openjdk  java-1.7.0-openjdk-devel
           

但是又出现了一些其他的环境缺少,总之将这些缺失的环境都安装起来就好了,下面是我遇到的一些其他的环境的确实,供参考

⑴:Could not find a C++ compiler

sudo yum groupinstall "Development Tools"
           

⑵:Could not find X11 libraries

sudo yum install libXtst-devel libXt-devel libXrender-devel
           

⑶:Could not find cups

sudo yum install cups-devel
           

⑷:Could not find freetype!

sudo yum install freetype-devel
           

⑸:Could not find alsa!

sudo yum install alsa-lib-devel
           

⑹:

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.

sudo yum install ccache
           

⑺:ccache status: installed, but disabled (version older than 3.1.4)

yum install ccache
           

基本都是在执行完bash ./configure之后,他会给你相应的提示,按照相应的提示进行操作就好了

4:开始编译

执行下面的这个命令

make all
           
执行的时候出现下面的问题

Starting langtools

/bin/sh: 第 0 行? /root/YourOpenJDK/langtools/make: 没有那个文件或目录

这个问题导致的原因可能是你在最初执行bash ./get_source.sh命令的时候出现了中断,然后部分的被回滚了,这样就不是最新的代码,可以不断的尝试bash ./get_source.sh命令直到没有出现异常回滚的情况

上面这个问题解决后,接着执行make all命令,正常情况下会开始编译了,最终出现下面的这个代码提示的时候就是完成编译了,编译的时长会有点长(我的机子性能不好,编译了快1个多钟头),慢慢等待编译完成就好了

----- Build times -------
Start 2019-02-27 01:16:42
End   2019-02-27 01:20:41
00:00:00 corba
00:00:01 demos
00:03:22 docs
00:00:01 hotspot
00:00:33 images
00:00:00 jaxp
00:00:01 jaxws
00:00:01 jdk
00:00:00 langtools
00:00:00 nashorn
00:03:59 TOTAL
-------------------------
Finished building OpenJDK for target 'all'
           

在编译完成后,编译出来的jdk和jre在/root/YourOpenJDK/build/linux-x86_64-normal-server-release/images(你自己的YourOpenJDK路径下面),然后可以进入images执行下面的命令

bin/java -version
           

就会出现下面的这个信息,这样基本就是说明你编译成功了,至此编译你的OpenJDK完成,恭喜

openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-root_2019_02_26_11_34-b00)
OpenJDK 64-Bit Server VM (build 25.0-b70, mixed mode)