Centos7 下编译 Openjdk8

本文主要介绍如何在 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
2
3
4
[root@45790fc652d5 ~]# java -version
openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-b16)
OpenJDK 64-Bit Server VM (build 25.141-b16, mixed mode)

2 下载openjdk1.8源码

首先安装 hd yum install hg,接着开始下载源码,网络不好的话,这个下载会特别慢!

1
2
3
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u openjdk8
cd openjdk8
sh get_source.sh

3 安装依赖

1
yum install alsa-lib-devel cups-devel libX* gcc gcc-c++ freetype-devel libstdc++-static ant make

4 编译配置

切换到 openjdk8 目录

1
2
chmod +x configure
./configure --enable-debug

5 编译

直接在openjdk8下执行make,控制台提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
[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=<config pattern> (or SPEC=<specfile>)
Makefile:55: *** Cannot continue. Stop.
```

我们只需要安装提示重新 `make CONF=linux-x86_64-normal-server-fastdebug` 就行了, make 过程比较费时间,可能提出类似如下错误提示

``` shell
[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)
```

刚开始吓了一跳,网上搜了下,这是个 bug[https://bugs.openjdk.java.net/browse/JDK-8016451](https://bugs.openjdk.java.net/browse/JDK-8016451) ,最后编译成功会看到控制台提示

``` console
## Finished jdk (build time 00:03:56)

----- Build times -------
Start 2017-09-04 11:46:36
End 2017-09-04 12:02:25
00:00:31 corba
00:09:53 hotspot
00:00:21 jaxp
00:00:29 jaxws
00:03:56 jdk
00:00:38 langtools
00:15:49 TOTAL
-------------------------
Finished building OpenJDK for target 'default'
```

我们再看下这个jdk的版本,果然和系统环境变量的不是一个哈

``` console
[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 的信息,代码如下

``` java
[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

``` shell
[root@45790fc652d5 ~]# javac JvmTest.java
[root@45790fc652d5 ~]# java JvmTest
/root
1.8.0_141
Linux
http://java.oracle.com/
```

- 使用自编译jdk

``` shell
[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/