【工欲善其事必先利其器·编译OpenJDK】编译OpenJDK

一、为什么

源码面前,了无秘密。要想深入学习java就需要需要深入源码深入底层,知其然知其所以然,编译OpenJDK有助于理解java语言的编译、执行等原理。本篇笔记不提供具体的执行过程(详细步骤后续笔记再记录),只提供一个基本的思路与方法。

二、怎么办

1、获取源码

源码下载地址:http://hg.openjdk.java.net/ (这是OpenJDK的hg仓库——类似git仓库,2019年11月份OpenJDK社区出现了一个新的JEP(JDK Enhancement Proposal) , 即JEP 369 : 把OpenJDK的源代码迁移到GitHub。不知道何时能够落地,如果迁移了那就更方便了,毕竟git更被人们所熟悉。)

选择您需要编译的版本,这里以jdk8u示例

http://hg.openjdk.java.net/ 页面点击jdk8u后进入以下页面:

再点击jdk8u进入以下页面:

 点击browse浏览该分支仓库中的文件:

 然后根据您的需求选择bz2、zip或gz下载对应的源码压缩包进行下载(这里选择gz),得到以下压缩包:

 2、阅读帮助文档

解压已下载的源码文件后得到如下文件:

进行到这一步时我们对编译OpenJDK依然一无所知,这时我们就需要利用好OpenJDK自带的文档来了解它了。

首先我们需要阅读源码跟目录下的README:

README:
  This file should be located at the top of the OpenJDK Mercurial root
  repository. A full OpenJDK repository set (forest) should also include
  the following 6 nested repositories:
    "jdk", "hotspot", "langtools", "corba", "jaxws"  and "jaxp".

  The root repository can be obtained with something like:
    hg clone http://hg.openjdk.java.net/jdk8/jdk8 openjdk8

这一小段翻译过来就是说:这个README文件应该位于OpenJDK Mercurial(hg)仓库的根目录下,一个完整的OpenJDK仓库应该包含类似以下6个的仓库:"jdk", "hotspot", "langtools", "corba", "jaxws"  and "jaxp",(在我们第一步获取的源码中似乎并没有出现这6个文件夹,难道下载错了?请继续以下步骤即可得到答案)

这里README还提供了另外一种下载源码的方法,前提是已安装好Mercurial(hg):

hg clone http://hg.openjdk.java.net/jdk8/jdk8 openjdk8

继续阅读下一段README:

  You can run the get_source.sh script located in the root repository to get
  the other needed repositories:
    cd openjdk8 && sh ./get_source.sh

  People unfamiliar with Mercurial should read the first few chapters of
  the Mercurial book: http://hgbook.red-bean.com/read/

  See http://openjdk.java.net/ for more information about OpenJDK.

您可以运行get_source.sh脚本来获取其他所需的仓库(上面提到的6个仓库):

cd openjdk8 && sh ./get_source.sh

如果您不熟悉Mercurial(hg)可以阅读其官方文档:http://hgbook.red-bean.com/read/

想要了解更多的关于OpenJDK的信息可以访问:http://openjdk.java.net/

使用hg下载太慢,建议从github下载:

https://github.com/unofficial-openjdk/openjdk 

继续阅读下一段README:

Simple Build Instructions:
  
  0. Get the necessary system software/packages installed on your system, see
     http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html

  1. If you don't have a jdk7u7 or newer jdk, download and install it from
     http://java.sun.com/javase/downloads/index.jsp
     Add the /bin directory of this installation to your PATH environment
     variable.

  2. Configure the build:
       bash ./configure
  
  3. Build the OpenJDK:
       make all
     The resulting JDK image should be found in build/*/images/j2sdk-image

where make is GNU make 3.81 or newer, /usr/bin/make on Linux usually
is 3.81 or newer. Note that on Solaris, GNU make is called "gmake".

Complete details are available in the file:
     http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html

简单的编译过程:

0.获取必须的系统软件或依赖包并将其安装到系统中,详细步骤请参考:http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html

1.如果您还没有安装jdk7u7 或更新的jdk,请下载并安装它:http://java.sun.com/javase/downloads/index.jsp 并将bin目录添加到您的系统环境变量中;

2.配置编译

bash ./configure

3.编译OpenJDK

make all

编译结果被村方到build/*/images/j2sdk-image目录下,编译成功后可查看。更有具体详细的编译请访问:http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html

发布了12 篇原创文章 · 获赞 26 · 访问量 3455

猜你喜欢

转载自blog.csdn.net/baidu_23747517/article/details/104234702