深入理解Java虚拟机 #01# 自己编译JDK

 

首先用书上的脚本尝试,失败。

之后根据源文件的 README 编译,抛出:

root@linux:/opt/openjdk# sh ./get_source.sh 
ERROR: Need initial repository to use this script

采用下面的方案解决:

你jdk8版本太老了,但是你系统又比较新(很多工具,例如make也是最新的),导致很多不匹配。解决办法换成最新版本的jdk8吧。
具体方法:hg clone http://hg.openjdk.java.net/jdk8u/jdk8u/ && sh get_source.sh. 后面就是一样的。 -- by baidu 乱乱我不乱 

需要注意,要使用 hg 必须先:

apt install mercurial

 

hg clone http://hg.openjdk.java.net/jdk9/jdk9 MyOpenJDK

cd MyOpenJDK

cat README

Welcome to OpenJDK!
===================

For information about building OpenJDK, including how to fully retrieve all
source code, please see either of these:

  * common/doc/building.html   (html version)
  * common/doc/building.md     (markdown version)

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

 cd ./common/doc/
firefox building.html

 cd /opt/MyOpenJDK/
sh get_source.sh

 bash configure

checking for X11/extensions/shape.h... no
configure: error: Could not find all X11 headers (shape.h Xrender.h XTest.h Intrinsic.h). You might be able to fix this by running 'sudo apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev'.
configure exiting with result code 1

apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev

bash configure

configure: error: Could not find cups! You might be able to fix this by running 'sudo apt-get install libcups2-dev'. 

 apt-get install libcups2-dev

在处理时有错误发生:
 oracle-java7-installer
E: Sub-process /usr/bin/dpkg returned an error code (1)

 bash configure

configure: error: Could not find freetype! You might be able to fix this by running 'sudo apt-get install libfreetype6-dev'.

apt-get install libfreetype6-dev

再次使用存在的到 download.oracle.com:80 的连接。
已发出 HTTP 请求,正在等待回应... 404 Not Found
2018-03-17 15:00:46 错误 404:Not Found。

download failed
Oracle JDK 7 is NOT installed.
dpkg: 处理软件包 oracle-java7-installer (--configure)时出错:
 子进程 已安装 post-installation 脚本 返回错误状态 1
正在设置 libfreetype6:amd64 (2.6.1-0.1ubuntu2.3) ...
正在设置 zlib1g-dev:amd64 (1:1.2.8.dfsg-2ubuntu4.1) ...
正在设置 libpng12-dev:amd64 (1.2.54-1ubuntu1) ...
正在设置 libfreetype6-dev:amd64 (2.6.1-0.1ubuntu2.3) ...
正在处理用于 libc-bin (2.23-0ubuntu5) 的触发器 ...
在处理时有错误发生:
 oracle-java7-installer
E: Sub-process /usr/bin/dpkg returned an error code (1)
# 1. oracle openjdk ppa source
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-7-jdk  // OpenJdk 7安装:

update-alternatives --config javac
update-alternatives --config java
改成 openJDK 7

再次  bash configure

====================================================
A new configuration has been successfully created in
/opt/MyOpenJDK/build/linux-x86_64-normal-server-release
using default settings.

Configuration summary:
* Debug level:    release
* HS debug level: product
* JDK variant:    normal
* JVM variants:   server
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 9-internal+0-adhoc.root.MyOpenJDK (9-internal)

Tools summary:
* Boot JDK:       openjdk version "1.8.0_151" OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12) OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)  (at /usr/lib/jvm/java-8-openjdk-amd64)
* Toolchain:      gcc (GNU Compiler Collection)
* C Compiler:     Version 5.4.0 (at /usr/bin/gcc)
* C++ Compiler:   Version 5.4.0 (at /usr/bin/g++)

Build performance summary:
* Cores to use:   3
* Memory limit:   3840 MB

 于是我 make images

root@linux:/opt/MyOpenJDK# make images
Building target 'images' in configuration 'linux-x86_64-normal-server-release'
/bin/bash: 第 0 行: cd: /opt/MyOpenJDK/hotspot/make: 没有那个文件或目录
make[2]: *** [hotspot-server-gensrc] Error 1
make[2]: *** 正在等待未完成的任务....
make/Main.gmk:255: recipe for target 'hotspot-server-gensrc' failed
Compiling 8 files for BUILD_TOOLS_LANGTOOLS

ERROR: Build failed for target 'images' in configuration 'linux-x86_64-normal-server-release' (exit code 2) 

=== Make failed targets repeated here ===
make/Main.gmk:255: recipe for target 'hotspot-server-gensrc' failed
=== End of repeated output ===

Hint: Try searching the build log for the name of the first failed target.
Hint: See common/doc/building.html#troubleshooting for assistance.

/opt/MyOpenJDK/make/Init.gmk:291: recipe for target 'main' failed
make[1]: *** [main] Error 1
/opt/MyOpenJDK/make/Init.gmk:185: recipe for target 'images' failed
make: *** [images] Error 2

(失败)

 再尝试 bash configure

WARNING: The result of this configuration has overridden an older
configuration. You *should* run 'make clean' to make sure you get a
proper build. Failure to do so might result in strange build problems.

x

我记得 opne jdk8u 目录下是有hotspot 的,但是jdk9 目录下找不到。正好没有把open jdk8u删掉,切进去试试。

/opt/jdk8u# bash configure 

通过配置检查,并得到下面的提示:

Build performance tip: ccache gives a tremendous speedup for C++ recompilations.
You do not have ccache installed. Try installing it.
You might be able to fix this by running 'sudo apt-get install ccache'.

apt-get install ccache(失败)

make images

警告: [options] 未与 -source 1.6 一起设置引导类路径
1 个警告
Generating linux_amd64_docs/jvmti.html
INFO: ENABLE_FULL_DEBUG_SYMBOLS=1
INFO: ALT_OBJCOPY=/usr/bin/objcopy
INFO: /usr/bin/objcopy cmd found so will create .debuginfo files.
INFO: STRIP_POLICY=min_strip
INFO: ZIP_DEBUGINFO_FILES=1
## Finished hotspot (build time 00:04:56)

## Starting corba
/bin/sh: 1: cd: can't cd to /opt/jdk8u/corba/make
/opt/jdk8u//make/Main.gmk:89: recipe for target 'corba-only' failed
make: *** [corba-only] Error 2

貌似是编译了一半。

再试一次 make all

root@linux:/opt/jdk8u# make all
Building OpenJDK for target 'all' in configuration 'linux-x86_64-normal-server-release'

## Starting langtools
## Finished langtools (build time 00:00:01)

## Starting hotspot
## Finished hotspot (build time 00:00:00)

## Starting corba
/bin/sh: 1: cd: can't cd to /opt/jdk8u/corba/make
/opt/jdk8u//make/Main.gmk:89: recipe for target 'corba-only' failed
make: *** [corba-only] Error 2

重头再来一遍,不行就用 vps 试一试。

hg clone http://hg.openjdk.java.net/jdk9/jdk9
cd jdk9
bash get_source.sh
bash configure
make images
./build/*/images/jdk/bin/java -version
make run-test-tier1

 还是失败了,get_source.sh 的时候报了很多类似这样的:

 abort: stream ended unexpectedly (got 115 bytes, expected 245)

还是一样的命令,换 vps :

get_source.sh 

WARNING: jaxws exited abnormally (255)
root@xkfx:/opt/jdk9# hg clone http://hg.openjdk.java.net/jdk9/jdk9/jaxws jaxws 
openjdk
—— corba:不流行的多语言、分布式通讯接口
—— hotspot:Java 虚拟机
—— jaxp:XML 处理
—— jaxws:一组 XML web services 的 Java API
—— jdk:java 开发工具包
—— —— 针对操作系统的部分
—— —— share:与平台无关的实现
—— langtools:Java 语言工具
—— nashorn:JVM 上的 JavaScript 运行时

来自 只会一点java

get_source 成功的话,目录应该是这样的

root@xkfx:/opt/jdk9# ls
ASSEMBLY_EXCEPTION  corba       jaxp   langtools  Makefile  test
common            get_source.sh  jaxws  LICENSE    nashorn
configure        hotspot       jdk      make         README

bash configure

configure: error: Could not find required tool for ZIPEXE
configure exiting with result code 1

apt-get install zip

bash configure

make clean 

make images

然后挂在后台编译,

root@xkfx:/opt/jdk9/build/linux-x86_64-normal-server-release/images/jdk/bin# ./java -version
openjdk version "9-internal"
OpenJDK Runtime Environment (build 9-internal+0-adhoc.root.jdk9)
OpenJDK 64-Bit Server VM (build 9-internal+0-adhoc.root.jdk9, mixed mode)

 

查看主要源代码:

sudo apt-get install tree

tree 以树状图显示所有文件
tree -L N 以树状图显示所有文件,子文件夹显示到第 N 层

root@xkfx:/opt/jdk9/hotspot/src# tree -L 1

.
├── cpu
├── jdk.aot
├── jdk.hotspot.agent
├── jdk.internal.vm.ci
├── jdk.internal.vm.compiler
├── os
├── os_cpu
└── share

posted @ 2018-03-17 21:31  xkfx  阅读(5002)  评论(2编辑  收藏  举报