【深入理解Java虚拟机】之自己编译JDK(已经完成,其中有自己遇到的各种问题及解决办法)

来源:互联网 发布:家居设计软件手机 编辑:程序博客网 时间:2024/05/21 18:55

(深入理解Java虚拟机 第二版 书中1.6 实战:自己编译JDK)
个人笔记,仅供参考
转载请注明:http://blog.csdn.net/u013190088/article/details/71703255

系统:CentOS 6.5
OpenJDK:OpenJDK 7u

1、获取源码。
两种方式:
① 直接官网下载
② 通过Mercurial代码版本管理工具获取。

说一下第二种方式:
首先安装Mercurial代码版本管理工具

yum install mercurial

安装之后clone源码

hg clone http://hg.openjdk.java.net/jdk7u/jdk7u-devcd jdk7u-devchmod 755 get_source.sh./get_source.sh

2、构建编译环境
需要准备一个6u14以上版本的JDK,以为OpenJDK的各个组成部分(Hotspot ,JDK API, JAXWS, JAXP…..)有的是使用C++编写的,更多的代码则是使用Java自身实现的,因此编译这些Java代码需要用到一个可用的JDK(Booststrap JDK)。
安装环境:

yum install build-essential gawk m4 openjdk-6-jdk libasound2-dev libcups2-dev libxrender-dev xorg-dev xutils-dev xllproto-print-dev binutils libmotif3 libmotif-dev ant

3 进行编译
需要对系统环境变量进行一些简单设置。OpenJDK在编译时读取的环境变量有很多,但大多都有默认值,必须设置的只有两个:LANG和ALT_BOOTDIR。
前者必须设置为:

export LANG=C

否则,在编译结束前的验证阶段会出现一个HashTable内的空指针异常。
另外一个ALT_BOOTDIR参数是前面提到的Booststrap JDK(最好不要用JDK1.8,否则后面编译的时候会报错!!!!见错误4)。

ALT_BOOTDIR=/usr/local/java/jdk1.8.0_101
#语言选项,这个必须设置,否则编译好后会出现一个HashTable的NPE错export LANG=C#Bootstrap JDK的安装路径。必须设置export ALT_BOOTDIR=/usr/local/java/jdk1.8.0_101 #允许自动下载依赖export ALLOW_DOWNLOADS=true#并行编译的线程数,设置为和CPU内核数量一致即可export HOTSPOT_BUILD_J0BS=6export ALT_PARALLEL_COMPILE_JOBS=6 #比较本次build出来的映像与先前版本的差异。这对我们来说没有意义, #必须设置为false,香则sanity检查会报缺少先前版本JDK的映像的错误提示。 #如桌已经设置dev或者DEV_ONLY=true,这个不显式设置也行export SKIP_COMPARE_IMAGES=true#使用预编译头文件,不加这个编译会更慢一些export USE_PRECOMPILED_HEADER=true#要编译的内容export BUILD_LANGTOOLS=true#export BUILD_JAXP=false#export BUILD_JAXWS=fa1se#export BUILD_CORBA=falseexport BUILD_HOTSPOT=trueexport BUILD_JDK=true#要编译的版本#export SKIP_DEBUG_BUILD=false#export SKIP_FASTDEBUG_BUILD=true#export DEBUG_NAME=debug#把它设置为false可以避开javaws和浏览器Java插件之类的部分的buildBUILD_DEPLOY=false#把它设置为false就不会build出安装包。因为安装包里有些奇怪的依赖, #但即便不build出它也已经能得到完整的JDK映像,所以还是别build它好了BUILD_INSTALL=false#编译结果所存放的路径export ALT_OUTPUTDIR=/home/openjdk/jdk7u-dev/build#这两个环境变量必须去掉,不然会有很诡异的事情发生(我没有具体查过这些 "诡异的#事情” ,Makefile脚本裣查到有这2个变量就会提示警告)unset JAVA_HOMEunset CLASSPATHmake 2>&1 | tee $ALT_OUTPUTDIR/build.log

make的时候会报错:
错误1:

ERROR: You do not have access to valid Cups header files.        Please check your access to            /usr/include/cups/cups.h        and/or check your value of ALT_CUPS_HEADERS_PATH,        CUPS is frequently pre-installed on many systems,        or may be downloaded from http://www.cups.org 

解决办法:

sudo yum install cups-devel.x86_64

错误2:

You seem to not have installed ALSA 0.9.1 or higher. 

解决办法:

sudo yum install alsa-lib-develUBUNTU下:sudo apt-get install libasound2-dev

错误3:

ERROR: FreeType version  2.3.0  or higher is required.

解决办法:

sudo yum install freetype-devel

此时make sanity提示通过。 可是make的时候还是报错:
错误4:

BUILD FAILED/home/openjdk/jdk7u-dev/langtools/make/build.xml:452: The following error occurred while executing this line:/home/openjdk/jdk7u-dev/langtools/make/build.xml:795: Compile failed; see the compiler error output for details.

解决办法:

更换ORACLE JDK版本 我换成了JDK1.7(之前为JDK1.8)export ALT_BOOTDIR=/home/openjdk/Downloads/jdk1.7.0_80/

make之后过了一阵时间没报错,以为成功了~~~又继续报错:
错误5:

BUILD FAILED/home/openjdk/jdk7u-dev/jaxp/build.xml:105: Problem: failed to create task or type replaceregexpCause: the class org.apache.tools.ant.taskdefs.optional.ReplaceRegExp was not found.        This looks like one of Ant's optional components.Action: Check that the appropriate optional JAR exists in        -/usr/share/ant/lib        -/home/openjdk/.ant/lib        -a directory added on the command line with the -lib argumentDo not panic, this is a common problem.The commonest cause is a missing JAR.This is not a bug; it is a configuration problem

解决办法:

sudo yum install ant*

错误6:

/usr/bin/ld: cannot find -lX11collect2: ld returned 1 exit status

解决办法:

sudo yum install libX11-devel(找不到其他库文件一样的解决办法,安装对应的库文件即可)

错误7:

make[5]: *** Waiting for unfinished jobs....

解决办法:

安装libxt-dev,不过yum源好像没有这包~rpm下载地址ftp://195.220.108.108/linux/centos/6.8/os/x86_64/Packages/libXt-devel-1.1.4-6.1.el6.x86_64.rpm下载后sudo rpm -ivh ****.rpm安装即可如果rpm安装提示pkgconfig(sm) is needed by ....安装libSM-devel即可(sudo yum install libSM-devel

错误8:

make[5]: Leaving directory `/home/openjdk/jdk7u-dev/jdk/make/sun/splashscreen'make[4]: *** [library_parallel_compile] Error 2make[4]: Leaving directory `/home/openjdk/jdk7u-dev/jdk/make/sun/splashscreen'make[3]: *** [all] Error 1make[3]: Leaving directory `/home/openjdk/jdk7u-dev/jdk/make/sun'make[2]: *** [all] Error 1make[2]: Leaving directory `/home/openjdk/jdk7u-dev/jdk/make'make[1]: *** [jdk-build] Error 2make[1]: Leaving directory `/home/openjdk/jdk7u-dev'make: *** [build_product_image] Error 2

解决办法:

sudo yum install libXtst-devel

错误9:

/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64.c:74: error: (Each undeclared identifier is reported only once/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64.c:74: error: for each function it appears in.)/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64.c:74: error: expected ';' before 'temp20'/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64.c:138: error: 'XRenderPictFormat' undeclared (first use in this function)/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64.c:138: error: expected ';' before 'temp52'/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64.c:438: error: 'temp20' undeclared (first use in this function)/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64.c:701: error: 'temp52' undeclared (first use in this function)/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64.c:1252: error: 'AwtGraphicsConfigData' has no member named 'renderPictFormat'make[4]: *** [/home/openjdk/jdk7u-dev/build/gensrc/sun/awt/X11/generator/sizer.64] Error 1make[4]: Leaving directory `/home/openjdk/jdk7u-dev/jdk/make/sun/xawt'make[3]: *** [all] Error 1make[3]: Leaving directory `/home/openjdk/jdk7u-dev/jdk/make/sun'make[2]: *** [all] Error 1make[2]: Leaving directory `/home/openjdk/jdk7u-dev/jdk/make'make[1]: *** [jdk-build] Error 2make[1]: Leaving directory `/home/openjdk/jdk7u-dev'make: *** [build_product_image] Error 2

解决办法:

sudo yum -y install libX*

终于编译好了!!!!

#-- Build times ----------Target all_product_buildStart 2017-05-13 15:37:57End   2017-05-13 15:43:0700:00:08 corba00:00:06 hotspot00:00:02 jaxp00:00:02 jaxws00:04:51 jdk00:00:01 langtools00:05:10 TOTAL

经过半天努力,终于搞出来了,测试一下:

[openjdk@hadoop j2sdk-image]$ pwd/home/openjdk/jdk7u-dev/build/j2sdk-image[openjdk@hadoop j2sdk-image]$ export JAVA_HOME=/home/openjdk/jdk7u-dev/build/j2sdk-image[openjdk@hadoop j2sdk-image]$ export PATH=$PATH:$JAVA_HOME/bin[openjdk@hadoop j2sdk-image]$ java -versionjava version "1.7.0_141"OpenJDK Runtime Environment (rhel-2.6.10.1.el6_9-x86_64 u141-b02)OpenJDK 64-Bit Server VM (build 24.141-b02, mixed mode)

运行虚拟机:
在product下边(看网上都是在jvmg下边,可我的jvmg下边没有gamma,不知道什么情况)的env.sh下加入:

LD_LIBRARY_PATH=.:${JAVA_HOME}/jre/lib/amd64/native_threads:${JAVA_HOME}/jre/lib/amd64:export LD_LIBRARY_PATH

使生效env.sh

source ./env.sh./gamma -version

如果报错:

./gamma: error while loading shared libraries: libjvm.so: cannot open shared object file: No such file or directory网上解法: 可以发现,其实在当前目录(也就是jvmg目录)就有对应的libjvm.so ,所以,修改   /etc/ld.so.conf  文件,把当前目录加进去。具体如下写道[zhenghui@localhost linux_amd64_compiler2]$ cat /etc/ld.so.confinclude ld.so.conf.d/*.conf/home/zhenghui/software/openjdk/build/linux_amd64_compiler2/jvmg

最后运行虚拟机成功!!

[openjdk@hadoop product]$ ./gamma -versionUsing java runtime at: /home/openjdk/jdk7u-dev/build/j2sdk-image/jreopenjdk version "1.7.0-internal"OpenJDK Runtime Environment (build 1.7.0-internal-openjdk_2017_05_13_11_17-b00)OpenJDK 64-Bit Server VM (build 24.80-b07, mixed mode)[openjdk@hadoop product]$ 
0 0