本文主要介绍如何在 Centos7 下编译 Openjdk8 源码,《深入理解java虚拟机》第二版网上好多 openjdk7 的帖子,编译 jdk8 和 7 还是有些差别的,比如大家经常提到的 make sanity 在 jdk8 源码里是没有的,下面我们一步步编译 openjdk8。
1 安装Bootstrap JDK
这里完全按照官方的说明来的。
1 |
yum install java-1.8.0-openjdk |
然后 java -version
看下已安装的 jdk 版本,确实是 1.8.0 哈
1 |
[root@45790fc652d5 ~]# java -version |
2 下载openjdk1.8源码
首先安装 hd yum install hg
,接着开始下载源码,网络不好的话,这个下载会特别慢!
1 |
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u openjdk8 |
3 安装依赖
1 |
yum install alsa-lib-devel cups-devel libX* gcc gcc-c++ freetype-devel libstdc++-static ant make |
4 编译配置
切换到 openjdk8 目录
1 |
chmod +x configure |
5 编译
直接在openjdk8下执行make
,控制台提示
[root@45790fc652d5 openjdk8]# make
No CONF given, but more than one configuration found in /app/openjdk8//build.
Available configurations:
* linux-x86_64-normal-server-fastdebug
* linux-x86_64-normal-server-release
Please retry building with CONF= (or SPEC=)
Makefile:55: *** Cannot continue. Stop.
我们只需要安装提示重新 make CONF=linux-x86_64-normal-server-fastdebug
就行了, make 过程比较费时间,可能提出类似如下错误提示
[root@45790fc652d5 openjdk8]# make
[Error] encoded value was less than 0: encode(-8.326673E-17, 5.0, 11.0, 16.0)
[Error] encoded value was less than 0: encode(-0.05882353, 1.0, 24.0, 25.0)
[Error] encoded value was greater than 3: encode(15.029411, 1.0, 14.0, 15.0)
[Error] encoded value was less than 0: encode(-0.05882353, 1.0, 24.0, 25.0)
[Error] encoded value was greater than 3: encode(15.029411, 1.0, 14.0, 15.0)
[Error] encoded value was less than 0: encode(-0.05882353, 1.0, 24.0, 25.0)
刚开始吓了一跳,网上搜了下,这是个 bughttps://bugs.openjdk.java.net/browse/JDK-8016451 ,最后编译成功会看到控制台提示
我们再看下这个jdk的版本,果然和系统环境变量的不是一个哈
[root@45790fc652d5 openjdk8]# build/linux-x86_64-normal-server-fastdebug/jdk/bin/java -version
openjdk version "1.8.0-internal-fastdebug"
OpenJDK Runtime Environment (build 1.8.0-internal-fastdebug-_2017_09_04_11_45-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00-fastdebug, mixed mode)
6 代码验证
我们写一段简单的 java 代码分别在两个 jdk 上编译 javac
并运行 java
以获取两个 jdk 的信息,代码如下
[root@45790fc652d5 ~]# cat JvmTest.java
public class JvmTest {
public static void main(String[] args) { System.out.println(System.getProperty("user.home")); System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("os.name")); System.out.println(System.getProperty("java.vendor.url")); } }
- 使用已安装jdk
[root@45790fc652d5 ~]# javac JvmTest.java
[root@45790fc652d5 ~]# java JvmTest
/root
1.8.0_141
Linux
http://java.oracle.com/
- 使用自编译jdk
[root@45790fc652d5 ~]# openjdk8/build/linux-x86_64-normal-server-fastdebug/jdk/bin/javac JvmTest.java
[root@45790fc652d5 ~]# openjdk8/build/linux-x86_64-normal-server-fastdebug/jdk/bin/java JvmTest
/root
1.8.0-internal-fastdebug
Linux
http://java.oracle.com/