1.修改ubuntu软件源
- 将ubuntu软件源配置文件sources.list修改为sources.list.bak
- 新建sources.list,将清华大学镜像粘贴进去
- 更新apt软件列表
cd /etc/apt/
mv sources.list sources.list.bak
cat > sources.list <<EOF
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
EOF
apt update
2.下载编译openjdk需要的依赖
《深入理解java虚拟机》给出的依赖为:
apt install build-essential gawk m4 openjdk-6-jdk libasound2-dev libcups2-dev libxrender-dev xorg-dev xutils-dev x11proto-print-dev binutils libmotif3 libmotif-dev ant
其中,openjdk-6-jdk 是不能直接apt安装的,我们可以直接到openjdk,选择合适的包下载。下载前还需要注册一个Oracle帐号。下载好jdk-6u45-linux-x64.bin后复制到想要的目录并运行,相同目录下就会创建一个包含有jdk的文件夹。
libmotif3也无法直接安装,在该网站我找到了答案:
This is a transitional package as the new packages libmrm4, libuil4
and libxm4 are binary compatible with libmotif3. It can safely be
removed.
libmotif3只是一个过渡产物,现在被libmrm4, libuil4, libxm4替代了。
网上查阅的依赖如下:
apt install build-essential gawk m4 libasound2-dev libcups2-dev libxrender-dev xorg-dev xutils-dev x11proto-print-dev binutils libmotif-common ant
apt install libmrm4 libuil4 libxm4
上面安装依赖的命令我都运行过
3.下载openjdk7源码
我是在gitee上找的源码:https://gitee.com/huan4j/jdk7.git
cd /usr
git clone https://gitee.com/huan4j/jdk7.git
mv jdk7 openjdk
4.安装openjdk6
第二步时下载的openjdk-6-jdk,拷贝到/usr/java目录下并执行
mkdir /usr/java
cd /usr/java
./jdk-6u45-linux-x64.bin
5.配置编译参数
修改配置文件:
vim /etc/profile
在后面追加如下内容:
##
export LANG=C
#BootStrap-JDK
export ALT_BOOTDIR=/usr/java/jdk1.6.0_45
#OracleJDK
export ALT_JDK_IMPORT_PATH=/usr/java/jdk1.6.0_45
#
export ALLOW_DOWNLOADS=true
#与你的CPU核数对应即可
export HOTSPOT_BUILD_JOBS=3
export ALT_PARALLEL_COMPILE_JOBS=3
#
export USE_PRECOMPILED_HEADER=true
#
export BUILD_LANGTOOLS=true
export BUILD_JAXWS=false
#export BUILD_JAXP=false
#export BUILD_CORBA=false
export BUILD_HOTSPOT=true
export BUILD_JDK=true
export SKIP_COMPARE_IMAGES=true
#
BUILD_DEPLOY=false
#
BUILD_INSTALL=false
#
export ALT_OUTPUTDIR=/usr/openjdk/build
#
unset JAVA_HOME
unset CLASSPATH
#
make 2>&1 | tee $ALT_OUTPUTDIR/build.log
6.编译源码
验证
cd /usr/openjdk/
make sanity
末尾出现Sanity check passed. 则验证通过。
编译
source /etc/profile
我给虚拟机分配了3核,编译耗时14分钟
7.验证编译结果
cd /usr/openjdk/build/j2sdk-image
ls -l