编译JDK17源码
maiaimei 2023/4/28 Java
# 在Linux上编译JDK17源码
安装Ubuntu 18
下载JDK17:https://www.oracle.com/java/technologies/downloads/ (opens new window)
下载JDK17源码包:https://jdk.java.net/ (opens new window)
解压JDK17源码包
unzip openjdk-17+35_src.zip
1
(可选步骤)卸载系统自带的JDK或之前安装的JDK,确保Boot JDK为16或17
安装jdk17,解压即可
tar -zxvf jdk-17_linux-x64_bin.tar.gz
1
安装JRE,主要是利用jlink去生成
cd jdk17解压目录
./bin/jlink --module-path jmods --add-modules java.desktop --output jre
1
2
2
配置环境变量,修改文件
vi /etc/profile
1
添加以下内容
JAVA_HOME=/opt/jdk-17.0.7
CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export PATH CLASSPATH JAVA_HOME
1
2
3
4
2
3
4
使环境变量生效
source /etc/profile
1
安装编译器gcc/g++/clang
gcc -v
apt install gcc -y
1
2
2
g++ -v
apt install g++ -y
1
2
2
clang --version
apt install clang -y
1
2
2
# 一次性安装gcc/g++/clang
apt install gcc g++ clang -y
1
2
2
安装make/autoconf/automake
make --version
autoconf --version
apt install make autoconf automake -y
1
2
3
2
3
进去JDK17源码包解压目录
cd openjdk
1
配置,用来生成 Makefile
sh configure
1
安装相关类库
# Could not find X11 libraries
apt-get install libx11-dev libxext-dev libxrandr-dev libxrender-dev libxtst-dev libxt-dev -y
# Could not find cups!
apt-get install libcups2-dev -y
# Could not find fontconfig!
apt-get install libfontconfig1-dev -y
# Could not find alsa!
apt-get install libasound2-dev -y
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
全量编译
make all
1
测试编译结果
/opt/openjdk/build/linux-x86_64-server-release/jdk/bin/java -version
1
编译成品在/opt/openjdk/build/linux-x86_64-server-release/images/jdk,
相当于https://www.oracle.com/java/technologies/downloads/ (opens new window)下载的免安装包
# 在Windows上编译JDK17源码(未通过)
cd /cygdrive/e/code/java-source-learning/jdk17/openjdk
# --with-toolchain-version:指定Microsoft Visual Studio版本
# --with-tools-dir:指定Microsoft Visual Studio C的库文件位置,这里使用转义路径会出问题
bash configure --with-toolchain-version=2019 --with-tools-dir=/cygdrive/d/MicrosoftVisualStudio/2019/Community/VC/Auxiliary
1
2
3
4
5
2
3
4
5