强烈建议初学者,用这个jdk源代码,按照下面的教程配置。其他源代码编译时,可能会出现各种古怪问题
####下载构建工具#######
sudo apt-get install build-essential gawk m4 openjdk-8-jdk libasound2-dev libcups2-dev libxrender-dev xorg-dev xutils-dev x11proto-print-dev binutils libmotif3 libmotif-div ant
请先阅读源码内的README-builds.html
进入openjdk 进行配置可以参阅 readme-beuilds.html中的配置。
####进行编译##########
####start 直接执行文件jdk-build.sh######
#!/bin/sh
#语言选项,这个必须设置,否则编译好后会出现hashtable的npe错
export LANG=C
#bootstrap jdk的安装路径,必须设置
export ALT_BOOTDIR=/usr/lib/jvm/jdk1.7.0_80
#允许自动下载依赖
export ALLOW_DOWNLOADS=true
#并行编译的线程数,设置为和cpu内核数量一样即可
export HOTSPOT_BUILD_JOBS=4
export ALT_PARALLEL_COMPILE_JOBS=4
#比较本次build出来的映像与之前版本的差异。这对我们来说没有意义
#必须设置为false,否则sanity检查会报缺少先前版本jdk的映像的错误提示
#如果已经设置dev或者dev_only=true,这个不显示设置也行
export SKIP_COMPARE_IMAGES=true
#使用预编译头文件,不加这个编译会更慢一些
export USE_PRECOMPILED_HEADER=true
#要编译的内容
export BUILD_LANGTOOLS=true
#export BUILD_JAXP=false
#export BUILD_JAXWS=false
#export BUILD_CORBA=false
export BUILD_HOTSPOT=true
export BUILD_JDK=true
#要编译的版本
#export SKIP_DEBUG_BUILD=false
#export SKIP_FASTDEBUG_BUILD=true
#export DEBUG_NAME=debug
#把它设置为false可以避开javaws和浏览器java插件之类的部分build
export BUILD_DEPLOY=false
#把它设置为false就不会build出安装包,因为安装包里有些奇怪的依赖
#但即便不build出它也已经能得到完整的jdk映像,所以还是别build好了
export BUILD_INSTALL=false
#编译结果所存放的路径
export ALT_OUTPUTDIR=/home/liurenjie/openjdk7/build
#这两个环境变量必须去掉,不然会出现很诡异的事情
unset JAVA_HOME
unset CLASSPATH
if [ -x "build" ] ;then
echo ***********start clean**************
make clean
else
echo ***********start build**************
make 2>&1|tee $ALT_OUTPUTDIR/build.log
fi
########结果################
#-- Build times ----------
Target all_product_build
Start 2017-04-09 15:12:41
End 2017-04-09 15:31:17
00:00:59 corba
00:03:08 hotspot
00:00:13 jaxp
00:00:16 jaxws
00:13:42 jdk
00:00:18 langtools
00:18:36 TOTAL
-------------------------
这样就是编译成功了。
#######build end##############
##########jdk#############
执行完后进入/openjdk7/build/j2sdk-image/bin
./java -version
openjdk version "1.7.0-internal"
OpenJDK Runtime Environment (build 1.7.0-internal-root_2017_04_09_15_12-b00)
OpenJDK 64-Bit Server VM (build 24.0-b56, mixed mode)
##########jdk编译成功############
遇到的问题总结:
1)#################
build-bootstrap-javac:
[javac] Compiling 111 source files to /home/liurenjie/openjdk7/build-debug/langtools/build/bootstrap/classes
[javac] /home/liurenjie/openjdk/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java:2182: warning: [overrides] Class Resolve.InapplicableSymbolsError.Candidate overrides equals, but neither it nor any superclass overrides hashCode method
[javac] private class Candidate {
[javac] ^
[javac] error: warnings found and -Werror specified
[javac] 1 error
[javac] 1 warning
BUILD FAILED
/home/liurenjie/openjdk/langtools/make/build.xml:452: The following error occurred while executing this line:
/home/liurenjie/openjdk/langtools/make/build.xml:795: Compile failed; see the compiler error output for details.
解决方案:换jdk
http://www.oracle.com/technetwork/Java/javase/downloads/java-archive-downloads-javase7-521261.html#jdk-7u80-oth-JPR oracle jdk7
2)###################
>&2 echo "*** This OS is not supported:" `uname -a`; exit 1;
“*** This OS is not supported:" 'uname -a'; exit 1;
解决办法
3)####################
2.各种.h找不到
解决办法
为bits,sys,gnu建立软链
sudo ln -s /usr/include/x86_64-linux-gnu/gun /usr/include/gnu
4)##########################
./gamma: relocation error: /usr/lib/jvm/jdk1.7.0_80/jre/lib/amd64/libjava.so: symbol JVM_FindClassFromCaller, version SUNWprivate_1.1 not defined in file libjvm.so with link time reference
修改文件:hotspot/make/linux/Makefile
去掉文件中所有的test_gamma即可
###不过在后续编译好之后,发现hotspot无法启动,提示找不到gamma 命令
5)######################
Error: time is more than 10 years from present: 1136059200000
java.lang.RuntimeException: time is more than 10 years from present: 1136059200000
at build.tools.generatecurrencydata.GenerateCurrencyData.makeSpecialCaseEntry(GenerateCurrencyData.java:285)
at build.tools.generatecurrencydata.GenerateCurrencyData.buildMainAndSpecialCaseTables(GenerateCurrencyData.java:225)
at build.tools.generatecurrencydata.GenerateCurrencyData.main(GenerateCurrencyData.java:154)
通过修改CurrencyData.properties文件, 把10年之前的时间修改为10年之内即可
6)#################
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 1073741840 bytes for Chunk::new
# An error report file with more information is saved as:
# /home/liu/openjdk7/jdk/make/com/sun/jmx/hs_err_pid27588.log
/home/liu/openjdk7/build/bin/java -XX:-PrintVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-LogVMOutput -Xmx512m -Xms512m -XX:PermSize=32m -XX:MaxPermSize=160m -cp /home/liu/openjdk7/build/classes sun.rmi.rmic.Main -classpath "/home/liu/openjdk7/build/classes" \
-d /home/liu/openjdk7/build/classes \
-iiop -v1.2 \
-standardPackage \
javax.management.remote.rmi.RMIConnectionImpl
Killed
###########log file###################
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 2147483664 bytes for Chunk::new
# Possible reasons:
# The system is out of physical RAM or swap space
# In 32 bit mode, the process size limit was hit
# Possible solutions:
# Reduce memory load on the system
# Increase physical memory or swap space
# Check if swap backing store is full
# Use 64 bit Java on a 64 bit OS
# Decrease Java heap size (-Xmx/-Xms)
# Decrease number of Java threads
# Decrease Java thread stack sizes (-Xss)
# Set larger code cache with -XX:ReservedCodeCacheSize=
# This output file may be truncated or incomplete.
#
# Out of Memory Error (allocation.cpp:323), pid=29957, tid=140531611686656
#
# JRE version: OpenJDK Runtime Environment (7.0) (build 1.7.0-internal-root_2017_03_30_20_45-b00)
# Java VM: OpenJDK 64-Bit Server VM (24.0-b56 mixed mode linux-amd64 compressed oops)
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again