我的技術博客:www.zhenganwen.topjava
Centos7
(能連外網)上編譯OpenJDK7u75
建議使用
Ubuntu
或者Centos
,安裝依賴的地方使用apt-get/yum
替換便可(Rhel
的yum
源使用收商業限制,所以依賴下載不方便,建議使用centos
或者debain
,或者更換yum
爲centos
也行,筆者起初在rhel
上編譯,踩了很多坑,最後換到centos
上才編譯成功。沒辦法,linux
是真的菜,不過經過此次採坑也學了很多linux
上編譯源碼的套路,本文會穿插着提到)。python
openjdk7u75,主角,下載好用來編譯。linux
在編譯JDK時不全是使用C/C++,也有用到JAVA的,所以須要一個已編譯好的JDK,官方稱其爲BOOTSTRAP JDK
,要求必須是JDK6 Update14
以上版本。建議到Oracle
官網上下載,筆者下載的是jdk-7u4-linux-x64
c++
編譯須要依賴GCC
、C++
等環境,一鍵下載:git
yum -y install build-essential gawk m4 openjdk6-jdk libasound2-print-dev binutils libmotif3 libmotif-dev ant
複製代碼
Xcode
相關的庫:github
yum install libX*
複製代碼
將下載好的openjdk
源碼和做爲bootstrap jdk
的jdk7u4
上傳到centos
的/usr/local/java
下並解壓:docker
[root@pinyoyougou-docker java]# ls
jdk1.7.0_04 jdk-7u4-linux-x64.tar.gz openjdk openjdk-7u75-src-b13-18_dec_2014.zip
複製代碼
上面的
openjdk
就是openjdk
源碼包解壓以後的目錄,記下這個名字,後續操做都是圍繞這個目錄展開的。shell
進入openjdk
並檢查編譯環境:bootstrap
[root@pinyoyougou-docker java]# cd openjdk
[root@pinyoyougou-docker java]# make sanity
WARNING: LANG has been set to zh_CN.UTF-8, this can cause build failures.
Try setting LANG to 'C'.
WARNING: The version of zip being used is older than
the required version of '2.2'.
The version of zip found was ''.
ERROR: The Compiler version is undefined.
ERROR: Your CLASSPATH environment variable is set. This will
most likely cause the build to fail. Please unset it
and start your build again.
ERROR: Your JAVA_HOME environment variable is set. This will
most likely cause the build to fail. Please unset it
and start your build again.
ERROR: You seem to not have installed ALSA 0.9.1 or higher.
Please install ALSA (drivers and lib). You can download the
source distribution from http://www.alsa-project.org or go to
http://www.freshrpms.net/docs/alsa/ for precompiled RPM packages.
ERROR: FreeType version 2.3.0 or higher is required.
make[2]: 進入目錄「/usr/local/java/openjdk/jdk/make/tools/freetypecheck」
/bin/mkdir -p /usr/local/java/openjdk/build/linux-amd64/btbins
rm -f /usr/local/java/openjdk/build/linux-amd64/btbins/freetype_versioncheck
make[2]: 離開目錄「/usr/local/java/openjdk/jdk/make/tools/freetypecheck」
Failed to build freetypecheck.
ERROR: You do not have access to valid Cups header files.
Please check your access to
/usr/include/cups/cups.h
and/or check your value of ALT_CUPS_HEADERS_PATH,
CUPS is frequently pre-installed on many systems,
or may be downloaded from http://www.cups.org
複製代碼
上面出現了兩個警告和五個異常,咱們須要逐個解決。(若是你make sanity
輸出的是check pass
則可跳過此步驟)centos
設置環境變量LANG=C
WARNING: LANG has been set to zh_CN.UTF-8, this can cause build failures.
Try setting LANG to 'C'.
複製代碼
解決方法:
[root@pinyoyougou-docker java]# export LANG=C
複製代碼
zip
版本較低,須要更新
WARNING: The version of zip being used is older than
the required version of '2.2'.
The version of zip found was ''.
複製代碼
解決方法:
yum install zip
複製代碼
缺乏編譯依賴
ERROR: The Compiler version is undefined.
複製代碼
解決:
[root@pinyoyougou-docker openjdk]# yum install gcc gcc-c++
複製代碼
取消已有的JDK
環境變量
ERROR: Your CLASSPATH environment variable is set. This will
most likely cause the build to fail. Please unset it
and start your build again.
複製代碼
因爲我以前在這個centos
上裝過jdk
並在/etc/profile
中配置過JAVA_HOME,CLASS_PATH
,所以這裏提示建議我暫時取消這兩項設置,不然可能出現未知錯誤。解決:
[root@pinyoyougou-docker openjdk]# unset JAVA_HOME
[root@pinyoyougou-docker openjdk]# unset CLASS_PATH
複製代碼
缺乏聲卡依賴alsa
,這個是linux
上經常使用的聲音設備的依賴,java.awt
須要依賴這個。
ERROR: You seem to not have installed ALSA 0.9.1 or higher.
Please install ALSA (drivers and lib). You can download the
source distribution from http://www.alsa-project.org or go to
http://www.freshrpms.net/docs/alsa/ for precompiled RPM packages.
複製代碼
解決
[root@pinyoyougou-docker openjdk]# yum search alsa
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.shu.edu.cn
* updates: mirrors.aliyun.com
======================================== N/S matched: alsa ========================================
alsa-firmware.noarch : Firmware for several ALSA-supported sound cards
alsa-lib.x86_64 : The Advanced Linux Sound Architecture (ALSA) library
alsa-lib.i686 : The Advanced Linux Sound Architecture (ALSA) library
alsa-lib-devel.i686 : Development files from the ALSA library
alsa-lib-devel.x86_64 : Development files from the ALSA library
alsa-plugins-arcamav.i686 : Arcam AV amplifier plugin for ALSA
alsa-plugins-arcamav.x86_64 : Arcam AV amplifier plugin for ALSA
alsa-plugins-maemo.i686 : Maemo plugin for ALSA
alsa-plugins-maemo.x86_64 : Maemo plugin for ALSA
alsa-plugins-oss.i686 : Oss PCM output plugin for ALSA
alsa-plugins-oss.x86_64 : Oss PCM output plugin for ALSA
alsa-plugins-pulseaudio.i686 : Alsa to PulseAudio backend
alsa-plugins-pulseaudio.x86_64 : Alsa to PulseAudio backend
alsa-plugins-samplerate.i686 : External rate converter plugin for ALSA
alsa-plugins-samplerate.x86_64 : External rate converter plugin for ALSA
alsa-plugins-upmix.i686 : Upmixer channel expander plugin for ALSA
alsa-plugins-upmix.x86_64 : Upmixer channel expander plugin for ALSA
alsa-plugins-usbstream.i686 : USB stream plugin for ALSA
alsa-plugins-usbstream.x86_64 : USB stream plugin for ALSA
alsa-plugins-vdownmix.i686 : Downmixer to stereo plugin for ALSA
alsa-plugins-vdownmix.x86_64 : Downmixer to stereo plugin for ALSA
alsa-tools.x86_64 : Specialist tools for ALSA
alsa-tools-firmware.x86_64 : ALSA tools for uploading firmware to some soundcards
alsa-utils.x86_64 : Advanced Linux Sound Architecture (ALSA) utilities
alsa-plugins-speex.i686 : Rate Converter Plugin Using Speex Resampler
alsa-plugins-speex.x86_64 : Rate Converter Plugin Using Speex Resampler
[root@pinyoyougou-docker openjdk]# yum -y install alsa-lib* alsa-util*
複製代碼
套路一:當提示缺乏依賴,而你不知道要
yum install
什麼時,你能夠根據提示關鍵字搜一下yum search
,而後在搜出的結果列表中,對有着相同前綴的依賴使用後綴通配符一鍵下載。
缺乏freetype
依賴
ERROR: FreeType version 2.3.0 or higher is required.
複製代碼
解決:
root@pinyoyougou-docker openjdk]# yum search freetype
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.shu.edu.cn
* updates: mirrors.aliyun.com
====================================== N/S matched: freetype ======================================
freetype-demos.x86_64 : A collection of FreeType demos
freetype-devel.i686 : FreeType development libraries and header files
freetype-devel.x86_64 : FreeType development libraries and header files
freetype.x86_64 : A free and portable font rendering engine
freetype.i686 : A free and portable font rendering engine
名稱和簡介匹配 only,使用「search all」試試。
[root@pinyoyougou-docker openjdk]# yum install freetype-devel.x86_64
複製代碼
套路二:使用
yum install
時習慣性帶上一個-y
,省得老是詢問是否肯定安裝。
因而我再make sanity
,發現還有一個問題
ERROR: You do not have access to valid Cups header files.
Please check your access to
/usr/include/cups/cups.h
and/or check your value of ALT_CUPS_HEADERS_PATH,
CUPS is frequently pre-installed on many systems,
or may be downloaded from http://www.cups.org
複製代碼
缺乏cups
打印框架,解決:
[root@pinyoyougou-docker openjdk]# yum search cups
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.shu.edu.cn
* updates: mirrors.aliyun.com
======================================== N/S matched: cups ========================================
bluez-cups.x86_64 : CUPS printer backend for Bluetooth printers
cups.x86_64 : CUPS printing system
cups-client.x86_64 : CUPS printing system - client programs
cups-devel.i686 : CUPS printing system - development environment
cups-devel.x86_64 : CUPS printing system - development environment
cups-filesystem.noarch : CUPS printing system - directory layout
cups-filters.x86_64 : OpenPrinting CUPS filters and backends
cups-filters-devel.i686 : OpenPrinting CUPS filters and backends - development environment
cups-filters-devel.x86_64 : OpenPrinting CUPS filters and backends - development environment
cups-filters-libs.i686 : OpenPrinting CUPS filters and backends - cupsfilters and fontembed
: libraries
cups-filters-libs.x86_64 : OpenPrinting CUPS filters and backends - cupsfilters and fontembed
: libraries
cups-ipptool.x86_64 : CUPS printing system - tool for performing IPP requests
cups-libs.x86_64 : CUPS printing system - libraries
cups-libs.i686 : CUPS printing system - libraries
cups-lpd.x86_64 : CUPS printing system - lpd emulation
ghostscript-cups.x86_64 : CUPS filter for interpreting PostScript and PDF
gutenprint-cups.x86_64 : CUPS drivers for Canon, Epson, HP and compatible printers
python-cups.x86_64 : Python bindings for CUPS
python-cups-doc.x86_64 : Documentation for python-cups
cups-pk-helper.x86_64 : A helper that makes system-config-printer use PolicyKit
foomatic-filters.x86_64 : CUPS print filters for the foomatic package
samba-krb5-printing.x86_64 : Samba CUPS backend for printing with Kerberos
名稱和簡介匹配 only,使用「search all」試試。
[root@pinyoyougou-docker openjdk]# yum install cups-devel.x86_64
複製代碼
make sanity
檢查經過:
Sanity check passed.
複製代碼
通過前面一番周折,編譯環境基本上準備好了,接下來就能夠make
進行編譯了,可是編譯前須要設置一下一些環境變量,諸如boostrap jdk
的根目錄在哪,哪些須要編譯,並行編譯線程數等。
在openjdk/
下建立compile.sh
並鍵入下列內容(參考《深刻理解Java虛擬機》):
#語言選項,這個必須設置,不然編譯好後會出現一個HashTable的NPE錯
export LANG=C
#Bootstrap JDK的安裝路徑。必須設置。
export ALT_BOOTDIR=/usr/local/java/jdk1.7.0_04
#容許自動下載依賴
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。
BUILD_DEPLOY=false
#把它設置爲false就不會build出安裝包。由於安裝包裏有些奇怪的依賴,但即使不build出它也已經能獲得完整的JDK映像,因此仍是別build它好了。
BUILD_INSTALL=false
#這兩個環境變量必須去掉,否則會有很詭異的事情發生(我沒有具體查過這些「」詭異的事情」,Makefile腳本檢查到有這2個變量就會提示警告「)
unset JAVA_HOME
unset CLASSPATH
make 2>&1 | tee $ALT_OUTPUTDIR/build.log
複製代碼
其中須要注意的地方:
LANG=C
、將ALT_BOOTDIR
設置成boostrap jdk
的根目錄jdk
環境變量,必定要在這裏unset
一下(上述的第40,41
行)cpu
核心個數相同便可其它的地方能夠照搬。
使用上面寫好的編譯啓動腳本開始編譯:
[root@pinyoyougou-docker openjdk]# chmod +x compile.sh
[root@pinyoyougou-docker openjdk]# ./compile.sh
複製代碼
這個過程一般須要幾十分鐘,若是一切順利,最後會顯示編譯開始時間以及終止時間。(固然,大部分狀況下,受挫之路至關曲折~)。
########################################################################
##### Leaving jdk for target(s) sanity all docs images #####
########################################################################
##### Build time 00:10:12 jdk for target(s) sanity all docs images #####
########################################################################
#-- Build times ----------
Target all_product_build
Start 2019-01-23 16:37:02
End 2019-01-23 16:48:00
00:00:23 corba
00:00:09 hotspot
00:00:03 jaxp
00:00:06 jaxws
00:10:12 jdk
00:00:03 langtools
00:10:58 TOTAL
-------------------------
make[1]: Leaving directory `/usr/local/java/openjdk'
[root@pinyoyougou-docker openjdk]#
複製代碼
因此下面將羅列筆者的受挫之路……若是make
過程當中發生error
異常終止,通常解決異常以後繼續make
。筆者開始傻到每次解決異常,而後make clean
,再make
。
cannot find -lstdc++
-l
表示庫,cannot find -lXXX
,一般表示缺乏libXXX
庫。看網上說是缺乏libstdc++-static
,但筆者建議最好將yum search libstdc*
的搜索結果一股腦全yum install
下來,反正又很少。
libjvm.so
軟鏈接死循環
/usr/bin/ld: cannot open output file libjvm.so: Too many levels of symbolic links
collect2: error: ld returned 1 exit status
ln: failed to access 'libjvm.so': Too many levels of symbolic links
ln: failed to access 'libjvm.so.1': Too many levels of symbolic links
/usr/bin/objcopy --only-keep-debug libjvm.so libjvm.debuginfo
/usr/bin/objcopy: Warning: could not locate 'libjvm.so'. reason: Too many levels of symbolic links
複製代碼
這時不能須要先make clean
,而後再make sanity
、make
超過十年問題
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)
複製代碼
修改openjdk//jdk/src/share/classes/java/util/CurrencyData.properties
,將其中表示年份的地方都改成距離今年不超過10年的年份:
缺乏C頭文件
fatal error: X11/Intrinsic.h: No such file or directory
# include <X11/Intrinsic.h>
複製代碼
/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory compilation terminated. make: *** [bitmap.o] Error 1
複製代碼
套路三:若是提示你缺乏
xxx/xxx.h
,可使用yum provides */xxx.h
查看該頭文件包含於哪一個庫中,再將該庫的全名稱做爲yum install
的參數下載
本想將筆者踩過的坑都貼出來,奈何編譯輸出信息太多難以找到採坑痕跡。總而言之,不是缺乏依賴,就是已有依賴不兼容(這時你須要32/64位版本都下載)
編譯成功以後,openjdk/build/linux-amd64/j2sdk-image
就是編譯好的jdk
的根目錄:
[root@pinyoyougou-docker j2sdk-image]# bin/java -version
openjdk version "1.7.0-internal"
OpenJDK Runtime Environment (build 1.7.0-internal-root_2019_01_23_14_04-b00)
OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)
複製代碼