Centos7 下编译 Openjdk8

Posted 孤独患者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
[[email protected] ~]# 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,控制台提示

[[email protected] 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 过程比较费时间,可能提出类似如下错误提示

[[email protected] 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 ,最后编译成功会看到控制台提示

## 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的版本,果然和系统环境变量的不是一个哈

[[email protected] 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 的信息,代码如下

[[email protected] ~]# 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
[[email protected] ~]# javac JvmTest.java 
[[email protected] ~]# java JvmTest      
/root
1.8.0_141
Linux
http://java.oracle.com/
  • 使用自编译jdk
[[email protected] ~]# openjdk8/build/linux-x86_64-normal-server-fastdebug/jdk/bin/javac JvmTest.java 
[[email protected] ~]# openjdk8/build/linux-x86_64-normal-server-fastdebug/jdk/bin/java JvmTest      
/root
1.8.0-internal-fastdebug
Linux
http://java.oracle.com/

以上是关于Centos7 下编译 Openjdk8的主要内容,如果未能解决你的问题,请参考以下文章

JAVA虚拟机学习笔记Windows10下编译OpenJDK8

JVM-Ubuntu18.04.1下编译OpenJDK8

[转]CentOS7下编译openjdk

CentOS7下编译Hadoop

centos7下编译安装nginx并实现日志轮替

centos7下编译Redis6.0