由于初次手动编译OpenJDK6, 踩的坑还不少, 各种依赖不足以及奇葩的事情, 特此整理记录, 给后人防坑.
编译环境信息:
[root@dev-52 ~]# uname -a
Linux dev-52 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux
[root@dev-52 openjdk6]# arch && cat /etc/centos-release
x86_64
CentOS release 6.2 (Final)
[root@dev-52 openjdk6]# nproc
8
[root@dev-52 ~]# free -m
total used free shared buffers cached
Mem: 7872 7695 176 0 158 6794
-/+ buffers/cache: 742 7129
Swap: 5823 0 5823
OpenJDK版本: openjdk-6-src-b27-26_oct_2012.tar.gz
下载地址: https://java.net/projects/openjdk6/downloads/
[root@dev-52 ~]# mkdir openjdk6 && tar xf openjdk-6-src-b27-26_oct_2012.tar.gz -C openjdk6
[root@dev-52 ~]# ll openjdk6
總計 320
-rw-rw-r-- 1 uucp 143 1503 2012-10-27 02:21 ASSEMBLY_EXCEPTION
drwxrwxr-x 4 uucp 143 4096 2012-10-27 02:28 corba
-rw-rw-r-- 1 uucp 143 1373 2012-10-27 02:21 get_source.sh
drwxrwxr-x 6 uucp 143 4096 2012-10-27 02:28 hotspot
drwxrwxr-x 5 uucp 143 4096 2012-10-27 02:28 jaxp
drwxrwxr-x 5 uucp 143 4096 2012-10-27 02:28 jaxws
drwxrwxr-x 5 uucp 143 4096 2012-10-27 02:28 jdk
drwxrwxr-x 5 uucp 143 4096 2012-10-27 02:29 langtools
-rw-rw-r-- 1 uucp 143 19263 2012-10-27 02:21 LICENSE
drwxrwxr-x 4 uucp 143 4096 2012-10-27 02:28 make
-rw-rw-r-- 1 uucp 143 14485 2012-10-27 02:21 Makefile
-rw-rw-r-- 1 uucp 143 1803 2012-10-27 02:21 README
-rw-rw-r-- 1 uucp 143 101427 2012-10-27 02:21 README-builds.html
-rw-rw-r-- 1 uucp 143 4807 2012-10-27 02:21 ReleaseProcess.html
drwxrwxr-x 2 uucp 143 4096 2012-10-27 02:28 test
-rw-rw-r-- 1 uucp 143 127498 2012-10-27 02:21 THIRD_PARTY_README
-rw-rw-r-- 1 uucp 143 2113 2012-10-27 02:21 TRADEMARK
# 安装依赖, 编译OpenJDK需要先安装一个JDK, 无论是OpenJDK还是Oracle JDK都行
[root@dev-52 ~]# yum install java-1.6.0-openjdk alsa-lib-devel gawk binutils libpng-devel freetype-devel openmotif openmotif-devel cups-devel ant libXtst-devel -y
# 将编译参数全部写到一个脚本中
[root@dev-52 openjdk6]# cd openjdk6
[root@dev-52 openjdk6]# vim build.sh
#!/bin/bash
export OPENJDK_HOME=/root/openjdk6 # 解压的源码包位置
export LANG=C
export ALT_BOOTDIR=/usr/lib/jvm/java-1.6.0-openjdk.x86_64 # 机器上现有的JDK位置, 下面一样
export ALT_JDK_IMPORT_PATH=/usr/lib/jvm/java-1.6.0-openjdk.x86_64
export ALT_OUTPUTDIR=$OPENJDK_HOME/build # 编译输出目录
export ALLOW_DOWNLOADS=true
export HOTSPOT_BUILD_JOBS=8 # 编译线程数, 与CPU核数一致即可
export ALT_PARALLEL_COMPILE_JOBS=8
export DEV_ONLY=true
export USE_PRECOMPILED_HEADER=true
export BUILD_LANGTOOLS=true
export BUILD_JAXP=false # 下面三个组件编译时需要从网络上下载相关依赖包, 耗时较长且对于作者没有任何用处就取消编译了
export BUILD_JAXWS=false
export BUILD_CORBA=false
export BUILD_HOTSPOT=true
export BUILD_JDK=true
export BUILD_DEPLOY=false # 取消构建部署和安装包
export BUILD_INSTALL=false
unset JAVA_HOME
unset CLASSPATH
make sanity # 验证
[root@dev-52 openjdk6]# chmod +x build.sh
[root@dev-52 openjdk6]# ./build.sh
# 由于输出内容过长占用篇幅较大就不完全贴出来了, 尾部的输出如下:
WARNING: You are not building the JAXP sources.
The jaxp files will be obtained from
the location set in ALT_JDK_IMPORT_PATH.
WARNING: You are not building the JAXWS sources.
The jaxws files will be obtained from
the location set in ALT_JDK_IMPORT_PATH.
WARNING: You are not building the CORBA sources.
The corba files will be obtained from
the location set in ALT_JDK_IMPORT_PATH.
WARNING: Your build environment has the variable DEV_ONLY
defined. This will result in a development-only
build of the JDK, lacking the documentation
build and installation bundles.
Sanity check passed. # 检查通过
# 修改build.sh的最后一行为如下, 然后在此执行脚本:
make sanity && make 2>&1 | tee $ALT_OUTPUTDIR/build.log
[root@dev-52 openjdk6]# ./build.sh
# 自2015年开始一般会出现如下异常
/opt/jdk1.6.0_31/bin/java -Xmx896m -Xms128m -XX:PermSize=32m -XX:MaxPermSize=160m -jar /root/openjdk6/build/btjars/generatecurrencydata.jar -o /root/openjdk6/build/lib/currency.data.temp \
< ../../../src/share/classes/java/util/CurrencyData.properties
Error: time is more than 10 years from present: 1120165200000
java.lang.RuntimeException: time is more than 10 years from present: 1120165200000
at build.tools.generatecurrencydata.GenerateCurrencyData.makeSpecialCaseEntry(GenerateCurrencyData.java:285)
at build.tools.generatecurrencydata.GenerateCurrencyData.buildMainAndSpecialCaseTables(GenerateCurrencyData.java:225)
at build.tools.generatecurrencydata.GenerateCurrencyData.main(GenerateCurrencyData.java:154)
make[4]: *** [/root/openjdk6/build/lib/currency.data] Error 1
# 通过修改CurrencyData.properties文件, 把10年之前的时间修改为10年之内即可
# 部分amd64架构的机器可能会出现如下异常
cd linux_amd64_compiler2/product && ./test_gamma
java full version "1.6.0_36-b36"
./gamma: relocation error: /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.36.x86_64/jre/lib/amd64/libjava.so: symbol JVM_FindClassFromCaller, version SUNWprivate_1.1 not defined in file libjvm.so with link time reference
# 主要是由于OpenJDK的一个bug导致的, 把OpenJDK替换为Oracle JDK即可, 注意同时修改build.sh中的内容
[root@dev-52 openjdk6]# vim `find . -name 'CurrencyData.properties'`
# 编译完成后末尾输出如下, 也可检查程序的退出码:
>>>Making sec-files-win @ Mon Sep 7 15:06:20 CST 2015 ...
>>>Making jgss-files @ Mon Sep 7 15:06:20 CST 2015 ...
>>>Finished making images @ Mon Sep 7 15:06:20 CST 2015 ...
make[2]: Leaving directory `/root/openjdk6/jdk/make'
make[1]: Leaving directory `/root/openjdk6'
Control linux amd64 1.6.0-internal build_product_image build finished: 15-09-07 15:06
Control linux amd64 1.6.0-internal all_product_build build finished: 15-09-07 15:06
Control linux amd64 1.6.0-internal all build finished: 15-09-07 15:06
[root@dev-52 openjdk6]# echo $?
0
# 退出码为0说明程序正常退出, 编译成功进入输出目录的j2sdk-image下, 即可看到编译好的JDK了
[root@dev-52 openjdk6]# cd build/j2sdk-image/bin
[root@dev-52 j2sdk-image]# ls
ASSEMBLY_EXCEPTION bin demo include jre lib LICENSE man sample src.zip THIRD_PARTY_README
[root@dev-52 bin]# ./java -version
openjdk version "1.6.0-internal"
OpenJDK Runtime Environment (build 1.6.0-internal-root_07_sep_2015_14_49-b00)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
到此整个编译过程就结束了, 当然第一次编译肯定不会这么顺利的完成, 操作时需要细心, 当然编译OpenJDK7也是大同小异, 在debain平台下依赖包的名字可能会有差异, 这需要注意一下.
OK, 对JVM感兴趣的同学赶紧亲自动手试一下吧.
参考资料
- 《深入理解Java虚拟机:JVM高级特性与最佳实践》 作者: 周志明
- http://blog.csdn.net/bysun2013/article/details/41984335
- http://openjdk.java.net/