JDK发布至今,大家的使用方式基本是从官网下载一个版本,然后直接安装(windows)或者解压至指定的目录,配置环境变量(linux),本文这里尝试从开源的OpenJDK自己编译出一个JDK,从而希望能够对其底层的一些能有更多的理解。
本文的所有操作,大部分参考官方最新的指导http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html完成
新版介绍
- 新版使用简单的“configure && make”即可完成
- GNU make需要3.8.1版本以上
- 多核的机器在编译的时候使用时间更短
- 显著减少了嵌套和递归的编译调用,同时减少子线程的创建,即优化了编译过程
- 不再支持Windows MKS
- Windows Visual Studio中的
vsvars*.bat
和vcvars*.bat
现在回自动运行 - 不再依赖ant
- 不再支持
ALT_*
的环境变量
构建步骤
获取源码
OpenJDK的源码现在使用的是Mercurial来管理的,如果没有用过,可以参考 新手指引或者查看 手册。
以下操作均在Ubuntu上演示
执行hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
root@ubuntu-blade2:/opt/open-compiler# hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
The program 'hg' is currently not installed. You can install it by typing:
apt-get install mercurial
root@ubuntu-blade2:/opt/open-compiler#
此处提示需要安装mercurial
,我们执行apt-get install mercurial
即可
update-alternatives: using /usr/bin/xterm to provide /usr/bin/x-terminal-emulator (x-terminal-emulator) in auto mode
update-alternatives: using /usr/bin/lxterm to provide /usr/bin/x-terminal-emulator (x-terminal-emulator) in auto mode
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
root@ubuntu-blade2:/opt/open-compiler# hg
Mercurial Distributed SCM
basic commands:
add add the specified files on the next commit
annotate show changeset information by line for each file
clone make a copy of an existing repository
commit 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 show revision history of entire repository or files
merge merge working directory with another revision
pull pull changes from the specified source
push push changes to the specified destination
remove remove the specified files on the next commit
serve start stand-alone webserver
status show changed files in the working directory
summary summarize working directory state
update update working directory (or switch revisions)
use "hg help" for the full list of commands or "hg -v" for details
root@ubuntu-blade2:/opt/open-compiler#
这里我们看到,hg
命令已支持,现在我们再执行hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
root@ubuntu-blade2:/opt/open-compiler# hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
requesting all changes
adding changesets
adding manifests
adding file changes
added 942 changesets with 1309 changes to 136 files
updating to branch default
82 files updated, 0 files merged, 0 files removed, 0 files unresolved
root@ubuntu-blade2:/opt/open-compiler#
执行如下命令
cd YourOpenJDK
bash ./get_source.sh
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK# bash ./get_source.sh
# Repositories: corba jaxp jaxws langtools jdk hotspot nashorn
corba: hg clone http://hg.openjdk.java.net/jdk8/jdk8/corba corba
jaxp: hg clone http://hg.openjdk.java.net/jdk8/jdk8/jaxp jaxp
jaxp: requesting all changes
corba: requesting all changes
jaxp: adding changesets
corba: adding changesets
Waiting 5 secs before spawning next background command.
...
...
...
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK#
我们可以随时执行bash ./get_source.sh
来获取、更新最新的源码
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK# ll
total 424
drwxr-xr-x 14 root root 4096 Aug 14 14:56 ./
drwxr-xr-x 3 root root 4096 Aug 14 14:54 ../
-rw-r--r-- 1 root root 1503 Aug 14 14:54 ASSEMBLY_EXCEPTION
drwxr-xr-x 6 root root 4096 Aug 14 14:54 common/
-rw-r--r-- 1 root root 1235 Aug 14 14:54 configure
drwxr-xr-x 6 root root 4096 Aug 14 14:56 corba/
-rw-r--r-- 1 root root 1384 Aug 14 14:54 get_source.sh
drwxr-xr-x 4 root root 4096 Aug 14 14:54 .hg/
-rw-r--r-- 1 root root 70 Aug 14 14:54 .hgignore
-rw-r--r-- 1 root root 12881 Aug 14 14:54 .hgtags
drwxr-xr-x 8 root root 4096 Aug 14 14:58 hotspot/
drwxr-xr-x 7 root root 4096 Aug 14 14:57 jaxp/
drwxr-xr-x 7 root root 4096 Aug 14 14:57 jaxws/
drwxr-xr-x 2 root root 4096 Aug 14 14:54 .jcheck/
drwxr-xr-x 7 root root 4096 Aug 14 15:04 jdk/
drwxr-xr-x 7 root root 4096 Aug 14 14:56 langtools/
-rw-r--r-- 1 root root 19263 Aug 14 14:54 LICENSE
drwxr-xr-x 6 root root 4096 Aug 14 14:54 make/
-rw-r--r-- 1 root root 6430 Aug 14 14:54 Makefile
drwxr-xr-x 13 root root 4096 Aug 14 14:56 nashorn/
-rw-r--r-- 1 root root 1549 Aug 14 14:54 README
-rw-r--r-- 1 root root 129333 Aug 14 14:54 README-builds.html
drwxr-xr-x 2 root root 4096 Aug 14 14:54 test/
-rw-r--r-- 1 root root 178445 Aug 14 14:54 THIRD_PARTY_README
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK#
构建
环境准备
- GNU make版本必须大于3.8.1,可以执行
make --version
来查看make版本
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK# make --version
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-pc-linux-gnu
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK#
安装一个启动JDK(Install a Bootstrap JDK
)。所有OpenJDK的编译均依赖一个称为bootstrap JDK或者boot JDK。注意本例中编译JDK8,我们的使用的boot JDK的版本不能高于8,建议使用JDK7的版本来作为boot JDK,以免引入不必要的JDK8的依赖。安装必须依赖软件
sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev libcups2-dev libasound2-dev ccache
编译
环境准备好之后,执行bash ./configure
,一切顺利的话
====================================================
A new configuration has been successfully created in
/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release
using default settings.
Configuration summary:
* Debug level: release
* JDK variant: normal
* JVM variants: server
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
Tools summary:
* Boot JDK: java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode) (at /opt/jdk-7u80-linux-x64)
* C Compiler: gcc-4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3) version 4.8.4 (at /usr/bin/gcc-4.8)
* C++ Compiler: g++-4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3) version 4.8.4 (at /usr/bin/g++-4.8)
Build performance summary:
* Cores to use: 16
* Memory limit: 96636 MB
* ccache status: not installed (consider installing)
执行make all
之前,请检查内存剩余是否大于1G,磁盘剩余空间是否大于5G,请尽量留多一些的空间,此过程会持续较长时间,请耐心等待
## Finished docs (build time 00:02:00)
----- Build times -------
Start 2018-08-14 17:04:51
End 2018-08-14 17:14:19
00:00:26 corba
00:00:07 demos
00:02:00 docs
00:02:34 hotspot
00:00:19 images
00:00:24 jaxp
00:00:30 jaxws
00:02:49 jdk
00:00:00 langtools
00:00:19 nashorn
00:09:28 TOTAL
-------------------------
Finished building OpenJDK for target 'all'
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK#
当看到如上提示的时候,表示已经编译完成
测试
编译出来的jdk和jre在
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images# ll
total 24020
drwxr-xr-x 9 root root 4096 Aug 14 17:12 ./
drwxr-xr-x 14 root root 4096 Aug 14 19:47 ../
drwxr-xr-x 5 root root 4096 Aug 14 19:47 j2re-image/
drwxr-xr-x 9 root root 4096 Aug 14 19:47 j2sdk-image/
drwxr-xr-x 4 root root 4096 Aug 14 17:12 lib/
-rw-r--r-- 1 root root 717434 Aug 14 17:12 sec-bin.zip
drwxr-xr-x 3 root root 4096 Aug 14 17:12 src/
-rw-r--r-- 1 root root 23836708 Aug 14 17:12 src.zip
drwxr-xr-x 4 root root 4096 Aug 14 17:12 _strip_jdk/
drwxr-xr-x 4 root root 4096 Aug 14 17:12 _strip_jre/
drwxr-xr-x 3 root root 4096 Aug 14 17:12 symbols/
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images#
分别为j2re-image/
和j2sdk-image/
,简单验证下
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images/j2sdk-image# bin/java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-root_2018_08_14_17_01-b00)
OpenJDK 64-Bit Server VM (build 25.0-b70, mixed mode)
至此完成!
后结
看过这个链接http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html#make的同学可能会发现,其最后一步Test如下
Testing
When the build is completed, you should see the generated binaries and associated files in the
j2sdk-image
directory in the output directory. In particular, thebuild/***/images/j2sdk-image/bin
directory should contain executables for the OpenJDK tools and utilities for that configuration. The testing tooljtreg
will be needed and can be found at: the jtreg site. The provided regression tests in the repositories can be run with the command:
**cd test && make PRODUCT_HOME=
pwd/../build/*/images/j2sdk-image all**
由于一直尝试未执行成功,所以就以简单的验证尝试了下,其一直出这个错
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/test# make PRODUCT_HOME=`pwd`/../build/*/images/j2sdk-image
make -k -C ../jdk/test TEST=jdk_core jdk_core
make[1]: Entering directory `/opt/open-compiler/YourOpenJDK/jdk/test'
echo "Running tests: jdk_core"
Running tests: jdk_core
for each in jdk_core; do \
make -j 1 TEST_SELECTION=":$each" UNIQUE_DIR=$each jtreg_tests; \
done
make[2]: Entering directory `/opt/open-compiler/YourOpenJDK/jdk/test'
make[2]: *** No rule to make target `/java/re/jtreg/4.1/promoted/latest/binaries/jtreg', needed by `/java/re/jtreg/4.1/promoted/latest/binaries/jtreg/win32/bin/jtreg'.
make[2]: Target `jtreg_tests' not remade because of errors.
make[2]: Leaving directory `/opt/open-compiler/YourOpenJDK/jdk/test'
make[1]: *** [jdk_core] Error 2
make[1]: Leaving directory `/opt/open-compiler/YourOpenJDK/jdk/test'
make: *** [jdk_core] Error 2
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/test#
java和jtreg版本如下
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images# jtreg -version
jtreg, version 4.1 src b02
Installed in /usr/share/java/jtreg.jar
Running on platform version 1.7.0_80 from /opt/jdk-7u80-linux-x64/jre.
Built with 1.6.0_23 on 07/28/2011 12:18 AM.
Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
Use is subject to license terms.
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images# java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images#
期间一直google了半天,未有解决方案,若哪位小伙伴解决这个问题,可以下面留言,先感谢一下!