最近在学习《深入理解java虚拟机 第二版》这本书。书中第一部分建议大家自己编译OpenJDK。抱着学习态度也来编译个玩一玩。下面进入正题。

1.编译环境介绍

操作系统 CentOS Linux release 7.1.1503 (Core)
Linux内核版本 3.10.0-229.el7.x86_64
gcc版本 4.8.2 20140120 (Red Hat 4.8.2-16) (GCC)
openJdk版本 openjdk-7u40

2.准备工作

2.1下载OpenJDK

在这里复制链接直接在linux中wget。
 
解压压缩包,将得到openjdk文件夹
unzip  openjdk-7u40-fcs-src-b43-26_aug_2013.zip
cd openjdk
这里有一份构建说明文档README-builds.html ,建议在windows下面看。后面步骤全靠它了。
 
2.2 下载依赖
具体的依赖要求说明都在README-builds.html 中了,这里直接来命令:
# yum install -y  glibc*
# yum install cups-devel
# yum install -y  alsa*
# yum install -y fontconfig*
# yum install zip*
# yum install  libstdc++-static
 
 
安装freetype
下载源码
解压源码,编译安装
# tar -zxvf freetype-2.3.12.tar.gz
# cd freetype-2.3.12
# ./configure && make && sudo -u root make install
 
下载并安装ANT
配置ANT环境变量
# vi /etc/profile
 加入如下内容:
export ANT_HOME=/opt/openJDKroom/apache-ant-1.9.6
export PATH=$PATH:$ANT_HOME/bin
# source /etc/profile
 
下载并安装CUPS
tar  -jxvf  cups-2.1.2-source.tar.bz2
# ./configure && make && make install 
 
2.3 屏蔽已经配置的JAVA_HOME
下面贴出README-builds.html中原文:
Note that some Linux systems have a habit of pre-populating your environment variables for you, for example JAVA_HOME might get pre-defined for you to refer to the JDK installed on your Linux system. You will need to unset JAVA_HOME. It's a good idea to run env and verify the environment variables you are getting from the default system settings make sense for building the OpenJDK.

先检查一下本地是否配置有JAVA_HOME

# env | grep JAVA_HOME 
如果有则屏蔽掉。
 
2.4 配置引导JDK
下面贴出README-builds.html中原文:
Bootstrap JDK
All OpenJDK builds require access to the previously released JDK 6, this is often called a bootstrap JDK. The JDK 6 binaries can be downloaded from Sun's JDK 6 download site. For build performance reasons is very important that this bootstrap JDK be made available on the local disk of the machine doing the build. You should always set ALT_BOOTDIR to point to the location of the bootstrap JDK installation, this is the directory pathname that contains a bin, lib, and include It's also a good idea to also place its bin directory in the PATH environment variable, although it's not required.
 
下载安装Bootstrap
这里下载JDK 6 Linux 64位jdk-6u45-linux-x64.bin
# chmod +x jdk-6u45-linux-x64.bin
# ./jdk-6u45-linux-x64.bin
 
安装完毕后,当前目录下会生成jdk1.6.0_45目录,配置ALT_BOOTDIR环境变量为该目录路径。在/etc/profile中增加如下配置(以实际为准):
export ALT_BOOTDIR=/opt/openJDKroom/jdk1.6.0_45
# source /etc/profile
 
2.5 配置环境变量ALT_CACERTS_FILE
 

Certificate Authority File (cacert)

See http://en.wikipedia.org/wiki/Certificate_Authority for a better understanding of the Certificate Authority (CA). A certificates file named "cacerts" represents a system-wide keystore with CA certificates. In JDK and JRE binary bundles, the "cacerts" file contains root CA certificates from several public CAs (e.g., VeriSign, Thawte, and Baltimore). The source contain a cacerts file without CA root certificates. Formal JDK builders will need to secure permission from each public CA and include the certificates into their own custom cacerts file. Failure to provide a populated cacerts file will result in verification errors of a certificate chain during runtime. The variable ALT_CACERTS_FILE can be used to override the default location of the cacerts file that will get placed in your build. By default an empty cacerts file is provided and that should be fine for most JDK developers.
进入openjdk目录中,查找名称为cacerts的文件:
# cd openjdk
# find ./ -name cacerts
显示结果如下:
[root@localhost openjdk]# find ./ -name cacerts
./jdk/src/share/lib/security/cacerts
打开/etc/profile,将以下内容加入(具体路径根据实际情况):
export ALT_CACERTS_FILE=/opt/openJDKroom/openjdk/jdk/src/share/lib/security/cacerts
 
 
3.开始编译
 终于要开始编译了!>_<
 README-builds.html中用的是gmake,实际和make是一样的,下面是解释:
 
gmake是GNU Make的缩写。
Linux系统环境下的make就是GNU Make,之所以有gmake,是因为在别的平台上,make一般被占用,GNU make只好叫gmake了。
进入到openjdk根目录下,进行编译
cd /opt/openJDKroom/openjdk
gmake sanity
gmake
结果执行报错:
gmake[6]: Entering directory `/opt/openJDKroom/openjdk/build/linux-amd64/hotspot/outputdir/linux_amd64_compiler2/product'
gmake[6]: stat: libjvm.so: Too many levels of symbolic links
 
进入到/opt/openJDKroom/openjdk/build/linux-amd64/hotspot/outputdir/linux_amd64_compiler2/product目录下有下面链接:
libjvm.so->libjvm.so
libjvm.so.1->libjvm.so
明显是由于libjvm.so->libjvm.so引起的(可以自己做一个实验,不要创建文件,直接执行ln -s file file ,然后cat file就会报这个错)
知道了问题所在,就要给它引用一个真实的so文件。通过find /opt -name libjvm.so命令查找存在该文件的地方,将libjvm.so->libjvm.so指向真实文件位置。即: 
# rm -f libjvm.so
# ln -s  具体libjvm.so文件完成路径   libjvm.so
 
再次执行gmake。
结果报以下错:
Error: time is more than 10 years from present: 1136059200000
java.lang.RuntimeException: time is more than 10 years from present: 1136059200000
        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)
 
 经过查询发现这是一个BUG.....
下面介绍了对应补丁的出处
下面有关于问题原因探讨描述
 
如果不想仔细看可以直接访问补丁网址:
将其中的文件内容拷贝下来覆盖到对应文件中。
 
继续gmake
 
终于编译成功了!!!
出现下面提示就说明编译完成了
#-- Build times ----------
Target all_product_build
Start 2016-01-10 11:18:43
End   2016-01-10 11:52:15
00:00:15 corba
00:00:21 hotspot
00:00:11 jaxp
00:00:20 jaxws
00:32:15 jdk
00:00:10 langtools
00:33:32 TOTAL
-------------------------
 
4.跑一下自己编译的虚拟机
进入目录
/opt/openJDKroom/openjdk/build/linux-amd64/hotspot/outputdir/linux_amd64_compiler2
下面有好几种优化级别的编译版本:
drwxr-xr-x. 2 root root  4096 Jan  9 16:53 debug
drwxr-xr-x. 2 root root  4096 Jan  9 16:53 fastdebug
drwxr-xr-x. 7 root root  4096 Jan  9 16:56 generated
drwxr-xr-x. 2 root root  4096 Jan  9 16:53 jvmg
drwxr-xr-x. 2 root root  4096 Jan  9 16:53 optimized
drwxr-xr-x. 3 root root 20480 Jan 10 11:19 product
drwxr-xr-x. 2 root root  4096 Jan  9 16:53 profiled
-rw-r--r--. 1 root root  1518 Jan  9 16:53 shared_dirs.lst
 
进入到product目录中。
这里要在env.sh配置下环境变量,指向共享库

LD_LIBRARY_PATH=.:${JAVA_HOME}/jre/lib/amd64/native_threads:${JAVA_HOME}/jre/lib/amd64:/opt/openJDKroom/openjdk/build/linux-amd64/hotspot/outputdir/linux_amd64_compiler2/product
export LD_LIBRARY_PATH

下面贴出我这个文件的完整内容:

# Generated by /opt/openJDKroom/openjdk/hotspot/make/linux/makefiles/buildtree.make
#: ${JAVA_HOME:=/opt/openJDKroom/jdk1.6.0_45}
JAVA_HOME=/opt/openJDKroom/openjdk/build/linux-amd64/j2sdk-image
CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/jre/lib/i18n.jar
HOTSPOT_BUILD_USER="root in hotspot"
export JAVA_HOME CLASSPATH HOTSPOT_BUILD_USER

# add

LD_LIBRARY_PATH=.:${JAVA_HOME}/jre/lib/amd64/native_threads:${JAVA_HOME}/jre/lib/amd64:/opt/openJDKroom/openjdk/build/linux-amd64/hotspot/outputdir/linux_amd64_compiler2/product
export LD_LIBRARY_PATH

下面执行:

# source ./env.sh

# ./gamma -version

Using java runtime at: /opt/openJDKroom/openjdk/build/linux-amd64/j2sdk-image/jre
openjdk version "1.7.0-internal"
OpenJDK Runtime Environment (build 1.7.0-internal-root_2016_01_10_00_16-b00)
OpenJDK 64-Bit Server VM (build 24.0-b56, mixed mode)

运行成功!!

希望对想要编译openJDK的朋友有个参考。如果有疑问请提出,大家一起学习探讨。>_<

JVM学习——编译OpenJDK的更多相关文章

  1. 深入理解JVM(一)编译openJDK

    此文总结的很不错:https://www.cnblogs.com/ACFLOOD/p/5528035.html 准备openJDK源码和环境 1.在linux和macOS上编译openJDK更加友好, ...

  2. 走进JVM之一 自己编译openjdk源码

    想要深入了解JVM,就必须了解其实现机制.了解JVM实现的最好方法便是自己动手编译JDK.好了,让我们开始吧! 1.  准备工作 获取OpenJDK源码 本次编译选择的是OpenJDK7u,官方源码包 ...

  3. 源码编译OpenJdk 8,Netbeans调试Java原子类在JVM中的实现(Ubuntu 16.04)

    一.前言 前一阵子比较好奇,想看到底层(虚拟机.汇编)怎么实现的java 并发那块. volatile是在汇编里加了lock前缀,因为volatile可以通过查看JIT编译器的汇编代码来看. 但是原子 ...

  4. JVM学习001通过实例总结Java虚拟机的运行机制

    JVM学习(1)——通过实例总结Java虚拟机的运行机制-转载http://www.cnblogs.com/kubixuesheng/p/5199200.html 文章转载自:http://www.c ...

  5. Linux CentOS 6.9 Minimal 编译 OpenJDK 7

    今天学习<深入理解Java虚拟机:JVM高级特性与最佳实践>一书,并动手在Linux系统上编译OpenJDK 7,初次搞不太顺利,特记录下编译操作细节. 一.前期准备 约定:工具默认安装目 ...

  6. Mac上编译OpenJDK过程记录

    编译OpenJDK主要为了学习HotSpot,编译过程在很多相关书籍中都有所涉及,但由于机型.机子具体环境的不同,很难有资料能够一步到位.还是得碰到具体问题通过上网查来一个个解决. 下载OpenJDK ...

  7. JVM学习笔记(一):JVM初探

    1 来源 来源:<Java虚拟机 JVM故障诊断与性能优化>--葛一鸣 章节:第一章 本文是第一章的一些笔记整理. 2 Java里程碑 2.1 Java起源 1990年Sun公司决定开发一 ...

  8. JVM学习笔记——类加载和字节码技术篇

    JVM学习笔记--类加载和字节码技术篇 在本系列内容中我们会对JVM做一个系统的学习,本片将会介绍JVM的类加载和字节码技术部分 我们会分为以下几部分进行介绍: 类文件结构 字节码指令 编译期处理 类 ...

  9. JVM学习(3)——总结Java内存模型

    俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: 为什么学习Java的内存模式 缓存一致性问题 什么是内存模型 JMM(Java Memory Model)简 ...

随机推荐

  1. 【翻译】How To Tango With Django 1.5.4 第三章

    django基础 3.1测试你的配置 测试你的python版本和你的django版本是否兼容 3.2新建django工程 在dos里面进到你事先新建的code文件夹,然后在执行下列命令新建工程 c:\ ...

  2. php插件开发

    新建config.m4 PHP_ARG_ENABLE(walu, [Whether to enable the "walu" extension], [ enable-waluEn ...

  3. 由FutureTask的get方法靠什么机制来阻塞引发的思考

    1. FutureTask的get方法靠什么机制来阻塞 看其get方法源码: /** * @throws CancellationException {@inheritDoc} */ public V ...

  4. javascript对json对象的序列化与反序列化

    首先引入一个json2.js.官方的地址为:https://github.com/douglascrockford/JSON-js 这里为了方便我直接贴上源代码 /* json2.js 2013-05 ...

  5. c++网络编程错误(WSAStartup)

    在使用CodeBlocks编译C++程序时, 编译程序的时候出现如下错误: undefined reference to `inet_addr@4'undefined reference to `ge ...

  6. http://www.miniui.com/demo/#src=datagrid/celledit.html

    http://www.miniui.com/demo/#src=datagrid/celledit.html   jQuery MiniUI Demo

  7. SQL中插入单引号,新增修改删除

    1.插入单引号如果不转化的话,字符串插入到数据库中错误的,只要在字符串中有单引号的地方在加一个单引号即可.    例如:在数据库插入'井下设备' :    insert into Static_Bel ...

  8. 在linux中访问virtualbox的共享文件夹

    1.在客户机里需要安装Virtualbox的增强功能. 2.使用virtualbox的图形界面设置好共享文件夹. 3.假设你设置的共享文件夹的名称是 share,使用如下命令在客户机的linux系统中 ...

  9. C++ 学习笔记(2) —— float 和 double 的精度

    Size Range Precision 4 bytes ±1.18 x 10-38 to ±3.4 x 1038 6-9 significant digits, typically 7 8 byte ...

  10. 十八、Java基础--------IO流体系以及字符流

    在上一章节中详细介绍集合框架的相关知识,在接下来的几篇文章中将讲述Java中另一个及其重要的知识——IO流,本文主要是讲述IO流的一些基本概念以及字符流的相关应用. IO流 介绍IO流之前先介绍一下什 ...