近期在研究jvm原理,看了不少书,总感觉停留在理论上,不能系统的学习以及深入理解。以及正所谓“纸上得来终觉浅,绝知此事要躬行”,要想深入的了解jvm原理,最起码要手动编译一个jdk以及虚拟机玩玩。本文总结一下自己在cenos6中编译openjdk8的一些粗浅经验,供大家参考学习。
1.操作系统说明
本文使用的cenos系统版本为cenos6.4 64位。可以使用如下的命令进行查看。
[root@localhost test]# uname -a Linux localhost 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
2.编译
2-1:获取源码
在本次的openjdk编译中,我们使用的版本为openjdk8.因此我们需要获取到openjdk8的源码。
openjdk8源码的获取方式有两种方式
1.使用hg工具进行源码的下载。
2.直接在官网进行下载源码包(下载地址)。
2-1-1 hg下载openjdk源码。
这种方式获取源码,比较耗时,下载的耗时时间,完全取决与我们的网速。在这里还是简单说明一下:
首先,我们需要下载Mercurial。详细步骤如下:
cd /var/local/ wget http://mercurial.selenic.com/release/mercurial-3.0.tar.gz tar xvzf mercurial-3.0.tar.gz mv mercurial-3.0 mercurial cd mercurial make all make install上述的命令执行完毕之后,如果没有错误,则输入hg -v命令之后,控制台显示的信息如下:
分布式软件配置管理工具 - 水银 基本命令: add add the specified files on the next commit annotate, blame show changeset information by line for each file clone make a copy of an existing repository commit, ci commit the specified files or all outstanding changes diff diff repository (or selected files) export dump the header and diffs for one or more changesets forget forget the specified files on the next commit init create a new repository in the given directory log, history show revision history of entire repository or files merge merge another revision into working directory pull pull changes from the specified source push push changes to the specified destination remove, rm remove the specified files on the next commit serve start stand-alone webserver status, st show changed files in the working directory summary, sum summarize working directory state
Mercurial安装完毕之后,我们就开始下载openjdk8的源码,命令如下:
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u jdk8u
cd jdk8u
bash get_source.sh经过一段耐心时间的等待,源码就会下载完毕。如果网速不给力的小朋友,可以直接通过我提供的下载地址下载即可。
3、openjdk8源码安装
yum makecache yum -y groupinstall 'base' yum groupinstall "Development Tools" yum install libXtst-devel libXt-devel libXrender-devel yum install cups-devel yum install freetype-devel yum install alsa-lib-devel大家可以一个个的下载安装即可。
3.2 环境准备
- The build is now a "
configure && make
" style build - Any GNU make 3.81 or newer should work
- The build should scale, i.e. more processors should cause the build to be done in less wall-clock time
- Nested or recursive make invocations have been significantly reduced, as has the total fork/exec or spawning of sub processes during the build
- Windows MKS usage is no longer supported
- Windows Visual Studio
vsvars*.bat
andvcvars*.bat
files are run automatically - Ant is no longer used when building the OpenJDK
- Use of ALT_* environment variables for configuring the build is no longer supported
3.3 make版本检查
[root@localhost openjdk-jdk8u-jdk8u]# make -v GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for x86_64-redhat-linux-gnu
3.4 引导启动jdk设置
在这里我使用的引导启动jdk版本为 jdk1.7.0_80ï¼ç®å½ä¸º /opt/jdk1.7.0_80。
4、开始编译openjdk8
cd openjdk bash ./configure --with-target-bits=64 --with-boot-jdk=/opt/jdk1.7.0_80 --with-debug-level=slowdebug --enable-debug-symbols ZIP_DEBUGINFO_FILES=0 make all ZIP_DEBUGINFO_FILES=0
参数说明:
--with-boot-jdk:指定引导JDK所在目录,以防其他安装的JDK影响(本机上以前安装了JDK8,并配置了JAVA_HOME指向JDK8);
--with-target-bits:指定编译64位系统的JDK;
为可以进行源码调试,再指定下面三个参数:
--with-debug-level=slowdebug:指定可以生成最多的调试信息;
--enable-debug-symbols ZIP_DEBUGINFO_FILES=0:生成调试的符号信息,并且不压缩;这个调试的时候非常有用。
命令最终执行完毕之后,即可完成编译,因为我实在虚拟机中进行openjdk8的编译,所以大概花费了2个小时。
5.测试编译的openjdk
编译的文件位于build/linux-x86_64-normal-server-slowdebug中,我们继续进入子目录,完整目录如下:
/opt/openjdk-jdk8u-jdk8u/build/linux-x86_64-normal-server-slowdebug/jdk/bin
输入java -version命令,效果如下所示:
[root@localhost bin]# java -version java version "1.8.0_162-ea" Java(TM) SE Runtime Environment (build 1.8.0_162-ea-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.162-b01, mixed mode)
ok,当看到上面的命令成功执行之后,便说明我们的openjdk8手工编译成功了。