深入理解Java虚拟机(1)-编译openjdk

想要窥探虚拟机内部的实现原理,最直接的一条路径就是编译一套自己的 jdk,通过阅读和调试 jdk 源码来了解 java 技术体系的运作,虽然这样门槛会高一点,但是肯定会比阅读各种文章、书籍来得更加贴近本质。此外,java 中许多方法都是 Native 的,在了解这些方法运作的过程都需要调试虚拟机代码的能力。

版本控制工具 Mecurial 常见命令

# 设置用户名和邮箱
hg config --edit

# 初始化仓库
hg init

# 查看项目状态
hg st

# 将文件添加到版本控制
hg add file_name

# 提交修改
hg ci -m "commit comment"

# 初次提交
hg push --new-branch

# 正常提交
hg push -b branch_name

# 新建分支
hg branch branch_name

# 查看当前分支
hg branch

# 查看所有分支
hg branches

# 切换分支
hg update new_branch

# 合并分支
hg merge branch

# 更新
hg pull

# 回退文件
hg revert file_name

# 全部回退
hg revert -a

获取源码

1、浏览器打开 OpenJDK / jdk8 / jdk8 ,因为种种原因,可能会比较慢;

2、点击页面左侧的 browse ,页面会列出 openjdk 的目录结构;

3、点击页面左边菜单栏 zip 👈 ,就能下载到源码的压缩包;

4、将源码压缩包解压缩到 jdk8-source 目录下。

配置本地 JDK

要拥有梯子,你必须要先有个梯子;要编译 JDK ,必须要有一个启动 JDK(Boot JDK)。本地 JDK 版本的与要编译的 JDK 版本有关系,略过了。

编译前准备

1、添加源码目录中的 .configure 执行权限;

2、执行 ./configure ,不出意外的话会报错,只能一个一个消灭这些错误,不过百度一下应该都能找到答案,要有耐心。

3、成功:

configure: creating /Users/chenxii/Documents/Java/compile-openjdk/jdk8-source/build/macosx-x86_64-normal-server-release/config.status
config.status: creating /Users/chenxii/Documents/Java/compile-openjdk/jdk8-source/build/macosx-x86_64-normal-server-release/spec.gmk
config.status: creating /Users/chenxii/Documents/Java/compile-openjdk/jdk8-source/build/macosx-x86_64-normal-server-release/hotspot-spec.gmk
config.status: creating /Users/chenxii/Documents/Java/compile-openjdk/jdk8-source/build/macosx-x86_64-normal-server-release/bootcycle-spec.gmk
config.status: creating /Users/chenxii/Documents/Java/compile-openjdk/jdk8-source/build/macosx-x86_64-normal-server-release/compare.sh
config.status: creating /Users/chenxii/Documents/Java/compile-openjdk/jdk8-source/build/macosx-x86_64-normal-server-release/spec.sh
config.status: creating /Users/chenxii/Documents/Java/compile-openjdk/jdk8-source/build/macosx-x86_64-normal-server-release/Makefile
config.status: creating /Users/chenxii/Documents/Java/compile-openjdk/jdk8-source/build/macosx-x86_64-normal-server-release/config.h

====================================================      * JVM variants:   server
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64

Tools summary:
* Boot JDK:       openjdk version "1.8.0_292" OpenJDK Runtime Environment (Zulu 8.54.0.21-CA-macosx) (build 1.8.0_292-b10) OpenJDK 64-Bit Server VM (Zulu 8.54.0.21-CA-macosx) (build 25.292-b10, mixed mode)  (at /opt/java/zulu8.54.0.21-ca-jdk8.0.292-macosx_x64)
* C Compiler:      version  (at /usr/bin/gcc)
* C++ Compiler:    version  (at /usr/bin/g++)

Build performance summary:
* Cores to use:   4
* Memory limit:   16384 MB
* ccache status:  installed, but disabled (version older than 3.1.4)

Build performance tip: ccache gives a tremendous speedup for C++ recompilations.


Build performance summary:
* Cores to use:   4
* Memory limit:   16384 MB
* ccache status:  installed, but disabled (version older than 3.1.4)

Build performance tip: ccache gives a tremendous speedup for C++ recompilations.
You have ccache installed, but it is a version prior to 3.1.4. Try upgrading.

常见错误汇总

1、configure: error: GCC compiler is required. Try setting –with-tools-dir.

注释掉 jdk8-source/common/autoconf/generated-configure.sh 中相关的地方,直接搜 GCC compiler is required ,然后将出现的行都用 # 注释掉。

2、error: could not found freetype

1、安装 freetype

2、./configure -with-freetype-include=/usr/local/include/freetype2 —with-freetype-lib=/usr/local/lib/

编译

1、执行 chmod a+x get_source.sh

2、执行 ./get_source.sh

2、执行 make all