不积跬步,无以至千里;不积小流,无以成江海。

CentOS7上编译JDK8

JAVA 康康 1227℃ 0评论

一、获取JDK8源码

两种方式获取源码

1.直接从openjdk资源库clone

yum install hg

克隆项目

hg clone http://hg.openjdk.java.net/jdk8/jdk8 my-open-jdk8

下载源码

cd my-open-jdk8

./get_source.sh

经测试这种方式相当慢。。不推荐

2.直接下载源码包zip格式,比较快 推荐

wget http://download.java.net/openjdk/jdk8u40/ri/openjdk-8u40-src-b25-10_feb_2015.zip

二、安装依赖,配置编译环境

1.README-builds 文档中列举的一些需要的依赖:

  • 机器内存最少1GB 磁盘空间最少6GB
  • Development Tools(也就是gcc make等一些工具,gcc版本要求3.7以上 freetype-devel 要求2.3以上)
  • Java Development ,也就是bootstrap jdk,必须是jdk7 , 版本最低为 JDK 7u7,因为jdk源码中有些功能是用java写的,所以编译中间需要用到java环境,就称为bootstrap jdk ,如果编译jdk7就用jdk6作为bootstrap jdk
  • X Software Development  (可能是编译出win的安装包需要这个)
  • cups devel: Cups Development Package
  • alsa devel: Alsa Development Package
  • Xi devel: libXi.so Development Package

2.安装这些依赖环境,推荐使用yum安装,比较方便:

yum -y install libX11-devel freetype-devel
yum -y groupinstall 'Development Tools'
yum -y install alsa*
yum -y install cups-devel ccache

3.配置编译环境

需要一个jdk7作为bootstrap jdk 解压备用

wget http://219.238.7.67/files/400800000A25538B/download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-x64.tar.gz

编辑/etc/profile :

#编译jdk需要设置的环境变量
#必须将 LANG设置为C
export LANG=C
#将我们刚刚解压的jdk配置到环境变量
export PATH="/software/java/jdk1.7.0_80/bin:${PATH}"

# 允许自动下载依赖
export ALLOW_DOWNLOADS=true

# 比较本次build出来的映像与先前版本的差异。
export SKIP_COMPARE_IMAGES=true

# 使用预编译头文件,不加这个编译会更慢一些
export USE_PRECOMPILED_HEADER=true

# 要编译的内容
export BUILD_LANGTOOLS=true
export BUILD_HOTSPOT=true
export BUILD_JDK=true

#把它设置为false可以避免javaws和浏览器java插件之类的部分的build
export BUILD_DEPLOY=false

# 把它设置为false就不会build出安装包。因为安装包里有一些奇怪的依赖
# 但即使不build出它也已经能得到完整的JDK映像,所以还是别build它好了
export BUILD_INSTALL=false

# 如果以前配置过这两个环境变量 一定要去掉
unset JAVA_HOME
unset CLASSPATH

编译JDK8同编译JDK7不同 , 废弃了一些 ALT_ 的一些参数

例如: HOTSPOT_BUILD_JOBS、ALT_PARALLEL_COMPILE_JOBS、ALT_OUTPUTDIR 无需再设置

三、开始编译

解压源码,cd到源码目录 。执行./configure ,笔者加了个参数开启了debug模式


[root@iZ2ze1np3s3kfsddhxqr0gZ openjdk]#./configure --with-debug-level=fastdebug

#根据检测的信息,如果出现错误,按提示安装需要的依赖即可
#一切正常,会打印出如下信息,列举了当前的配置信息
A new configuration has been successfully created in
/software/java/my-openjdk/openjdk/build/linux-x86_64-normal-server-fastdebug
using configure arguments '--with-debug-level=fastdebug'.

Configuration summary:
* Debug level:    fastdebug
* 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 /software/java/jdk1.7.0_80)
* C Compiler:     gcc (GCC) 4.4.7 20120313 (Red Hat-18) version 4.4.7-18) (at /usr/bin/gcc)
* C++ Compiler:   g++ (GCC) 4.4.7 20120313 (Red Hat-18) version 4.4.7-18) (at /usr/bin/g++)

Build performance summary:
* Cores to use:   2
* Memory limit:   15951 MB
* ccache status:  installed and in use

如果alsa freetype boot-jdk 等依赖不是yum装的 或者没有加入到环境变量,则需要configure参数来指定安装位置

执行编译

[root@iZ2ze1np3s3kfsddhxqr0gZ openjdk]#make 

等待20分钟左右,编译成功在build目录下生成 linux-x86_64-normal-server-fastdebug目录

测试下:

[root@iZ2ze1np3s3kfsddhxqr0gZ build]# cd linux-x86_64-normal-server-fastdebug/jdk/bin/
[root@iZ2ze1np3s3kfsddhxqr0gZ bin]# ./java -version
openjdk version "1.8.0-internal-fastdebug"
OpenJDK Runtime Environment (build 1.8.0-internal-fastdebug-root_2017_09_14_14_16-b00)
OpenJDK 64-Bit Server VM (build 25.0-b70-fastdebug, mixed mode)
[root@iZ2ze1np3s3kfsddhxqr0gZ bin]# 

四、附 录

关于configure的一些参数说明:

--enable-debug 开启dubug模式

--with-debug-level=level  debug模式的等级

--with-alsa=path  添加依赖,指定alsa的位置(yum安装的alsa不需要用这个指定)

--with-boot-jdk=path boot-jdk的位置(我们已配置到环境变量也无需指定)

--with-boot-jdk-jvmargs=“args” boot-jdk的jvm参数

--with-cups=path cups的位置

--with-freetype=path freetype的位置

--with-target-bits=arg  如果编译32位 可指定为32

--with-memory-size=size  指定GNU编译时 所需的RAM大小

更多请参考 : README-builds

编译中出现的错误:

Generating beaninfo
Generating Nimbus source files
[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)
[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)
[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)
[Error] Encountered Infinity: encode(-0.00877193, 0.0, 7.0, 7.0)

查询下,这是个官方的bug,似乎不影响结果 : https://bugs.openjdk.java.net/browse/JDK-8016451

资源下载:

源码下载地址:jdk8 jdk7

编译参考文档:http://openjdk.java.net/groups/build/

转载请注明:左手代码右手诗 » CentOS7上编译JDK8

喜欢 (3)or分享 (0)
头像
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
隐藏