1. 程式人生 > >CentOS 6.9 Minimal 編譯OpenJDK 7

CentOS 6.9 Minimal 編譯OpenJDK 7

建議 定向 mic emp nta 生效 reads TP lease

今天學習《深入理解Java虛擬機:JVM高級特性與最佳實踐》一書,並動手在Linux系統上編譯OpenJDK 7,初次搞不太順利,特記錄下編譯操作細節。

一、前期準備

約定:工具默認安裝目錄是/usr/local/,若有源碼,則源碼存放在/usr/local/src/目錄下。

1、OpenJDK

下載OpenJDK 7,地址:http://download.java.net/openjdk/jdk7/promoted/b147/openjdk-7-fcs-src-b147-27_jun_2011.zip,文件校驗碼:http://download.java.net/openjdk/jdk7/promoted/b147/openjdk-7-fcs-src-b147-27_jun_2011.md5

wget http://download.java.net/openjdk/jdk7/promoted/b147/openjdk-7-fcs-src-b147-27_jun_2011.zip   # 下載

unzip openjdk-7-fcs-src-b147-27_jun_2011.zip   # 解壓

mv openjdk openjdk7  # 更改目錄名

2、FreeType

筆者使用的是freetype-2.4.0.tar.gz,下載地址:https://sourceforge.net/projects/freetype/files/freetype2/2.4.0/

tar -zxvf freetype-2.4.0.tar.gz   # 解壓

cd freetype-2.4.0

./configure && make && make install   # 編譯安裝

3、Apache Ant

筆者使用的是apache-ant-1.9.7-bin.tar.gz,下載地址:https://archive.apache.org/dist/ant/binaries/

tar -zxvf apache-ant-1.9.7-bin.tar.gz  # 解壓

mv apache-ant-1.9.7 /usr/local/

ln -s /usr/local/apache-ant-1.9.7/bin/ant /usr/bin/ant  # 軟鏈接到bin

4、BootstrapJDK

筆者使用的是jdk-6u45-linux-x64,因為源碼更多是使用Java寫的,因此需要使用一個可用的JDK去編譯(詳見深入理解Java虛擬機第一章),OpenJDK解壓目錄下有個README-builds.html<OpenJDK Build README>,建議詳看一遍,文檔說使用JDK 6

下載jdk-6u45-linux-x64.bin,下載地址:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html

./jdk-6u45-linux-x64.bin   # 解壓

mv jdk1.6.0_45 /usr/local/  # 移動目錄

5、編譯需要的相關工具

yum -y install gcc gcc-c++ alsa-lib alsa-lib-devel libXrender libXrender-devel libXi-devel libXt-devel libXtst-devel cups cups-devel

二、配置環境變量

vim /etc/profile  # 在末尾加入以下內容註意各個目錄Path

# openjdk 7 build
export ANT_HOME=/usr/local/apache-ant-1.9.7
export ALT_FREETYPE_HEADERS_PATH=/usr/local/include/freetype2
export ALT_FREETYPE_LIB_PATH=/usr/local/lib
export ALT_DROPS_DIR=/usr/local/src/openjdk7/drop
export ALT_BOOTDIR=/usr/local/jdk1.6.0_45
export ALT_JDK_IMPORT_PATH=/usr/local/jdk1.6.0_45
export ALT_OUTPUTDIR=/usr/local/openjdk7
export LANG=C
export HOTSPOT_BUILD_JOBS=8
export ALT_PARALLEL_COMPILE_JOBS=8
export SKIP_COMPARE_IMAGES=true
export USE_PRECOMPILED_HEADER=true
export ALLOW_DOWNLOADS=true
export SKIP_DEBUG_BUILD=false
export SKIP_FASTDEBUG_BUILD=true
export DEBUG_NAME=debug
unset JAVA_HOME
unset CLASSPATH

  :wq  # 寫入磁盤

  source /etc/profile  # 刷新環境變量配置使之生效,建議echo下幾個配置項確認生效,例:echo $LANG

  cd openjdk7  # 進入OpenJDK解壓目錄

  make sanity   # 檢查之前的配置是否正確,若無錯誤(Sanity check passed.)則OK,下面是筆者的執行結果宮參考

技術分享圖片
[root@dsp-centos-3-6_9 openjdk7]# make sanity
( cd  ./jdk/make &&           make sanity HOTSPOT_IMPORT_CHECK=false JDK_TOPDIR=/usr/local/src/openjdk7/jdk JDK_MAKE_SHARED_DIR=/usr/local/src/openjdk7/jdk/make/common/shared EXTERNALSANITYCONTROL=true SOURCE_LANGUAGE_VERSION=7 TARGET_CLASS_VERSION=7 MILESTONE=internal BUILD_NUMBER=b00 JDK_BUILD_NUMBER=b00 FULL_VERSION=1.7.0-internal-debug-root_2018_05_01_00_10-b00 PREVIOUS_JDK_VERSION=1.6.0 JDK_VERSION=1.7.0 JDK_MKTG_VERSION=7 JDK_MAJOR_VERSION=1 JDK_MINOR_VERSION=7 JDK_MICRO_VERSION=0 PREVIOUS_MAJOR_VERSION=1 PREVIOUS_MINOR_VERSION=6 PREVIOUS_MICRO_VERSION=0 ARCH_DATA_MODEL=64 VARIANT=DBG DEBUG_CLASSFILES=true COOKED_BUILD_NUMBER=0 ANT_HOME="/usr/local/apache-ant-1.9.7" ALT_OUTPUTDIR=/usr/local/openjdk7 ALT_LANGTOOLS_DIST=/usr/local/openjdk7/langtools/dist ALT_CORBA_DIST=/usr/local/openjdk7/corba/dist ALT_JAXP_DIST=/usr/local/openjdk7/jaxp/dist ALT_JAXWS_DIST=/usr/local/openjdk7/jaxws/dist ALT_HOTSPOT_IMPORT_PATH=/usr/local/openjdk7/hotspot/import BUILD_HOTSPOT=true ; )
make[1]: Entering directory `/usr/local/src/openjdk7/jdk/makemake[1]: Leaving directory `/usr/local/src/openjdk7/jdk/make
Build Machine Information:
   build machine = dsp-centos-3-6_9

Build Directory Structure:
   CWD = /usr/local/src/openjdk7
   TOPDIR = .
   LANGTOOLS_TOPDIR = ./langtools
   JAXP_TOPDIR = ./jaxp
   JAXWS_TOPDIR = ./jaxws
   CORBA_TOPDIR = ./corba
   HOTSPOT_TOPDIR = ./hotspot
   JDK_TOPDIR = ./jdk

Build Directives:
   BUILD_LANGTOOLS = true 
   BUILD_JAXP = true 
   BUILD_JAXWS = true 
   BUILD_CORBA = true 
   BUILD_HOTSPOT = true 
   BUILD_JDK    = true 
   DEBUG_CLASSFILES =  
   DEBUG_BINARIES =  

Hotspot Settings: 
      HOTSPOT_BUILD_JOBS  = 8 
      HOTSPOT_OUTPUTDIR   = /usr/local/openjdk7/hotspot/outputdir 
      HOTSPOT_EXPORT_PATH = /usr/local/openjdk7/hotspot/import 
 



Bootstrap Settings:
  BOOTDIR = /usr/local/jdk1.6.0_45
    ALT_BOOTDIR = /usr/local/jdk1.6.0_45
  BOOT_VER = 1.6.0 [requires at least 1.6]
  OUTPUTDIR = /usr/local/openjdk7
    ALT_OUTPUTDIR = /usr/local/openjdk7
  ABS_OUTPUTDIR = /usr/local/openjdk7
 
Build Tool Settings:
  SLASH_JAVA = /NOT-SET
    ALT_SLASH_JAVA = 
  VARIANT = DBG
  JDK_DEVTOOLS_DIR = /NOT-SET/devtools
    ALT_JDK_DEVTOOLS_DIR = 
  ANT_HOME = /usr/local/apache-ant-1.9.7
  UNIXCOMMAND_PATH = /bin/
    ALT_UNIXCOMMAND_PATH = 
  COMPILER_PATH = /usr/bin/
    ALT_COMPILER_PATH = 
  DEVTOOLS_PATH = /usr/bin/
    ALT_DEVTOOLS_PATH = 
  UNIXCCS_PATH = /usr/ccs/bin/
    ALT_UNIXCCS_PATH = 
  USRBIN_PATH = /usr/bin/
    ALT_USRBIN_PATH = 
  COMPILER_NAME = GCC4
  COMPILER_VERSION = GCC4
  CC_VER = 4.4.7 [requires at least 4.3.0]
  ZIP_VER = 3.0 [requires at least 2.2]
  UNZIP_VER = 6.00 [requires at least 5.12]
  ANT_VER = 1.9.7 [requires at least 1.7.1]
  TEMPDIR = /usr/local/openjdk7/tmp
 
Build Directives:
  OPENJDK = true
  USE_HOTSPOT_INTERPRETER_MODE = 
  PEDANTIC = 
  DEV_ONLY = 
  NO_DOCS = 
  NO_IMAGES = 
  TOOLS_ONLY = 
  INSANE = 
  COMPILE_APPROACH = parallel
  PARALLEL_COMPILE_JOBS = 8
    ALT_PARALLEL_COMPILE_JOBS = 8
  FASTDEBUG = 
  COMPILER_WARNINGS_FATAL = false
  COMPILER_WARNING_LEVEL = 
  SHOW_ALL_WARNINGS = 
  INCREMENTAL_BUILD = false
  CC_HIGHEST_OPT = 
  CC_HIGHER_OPT = 
  CC_LOWER_OPT = 
  CXXFLAGS =  -g -fPIC -DCC_NOEX -W -Wall  -Wno-unused -Wno-parentheses -fno-omit-frame-pointer -D_LITTLE_ENDIAN  
  CFLAGS =  -g   -fno-strict-aliasing -fPIC -W -Wall  -Wno-unused -Wno-parentheses -pipe -fno-omit-frame-pointer -D_LITTLE_ENDIAN  
  BOOT_JAVA_CMD = /usr/local/jdk1.6.0_45/bin/java -XX:-PrintVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-LogVMOutput -Xmx512m -Xms512m -XX:PermSize=32m -XX:MaxPermSize=160m
  BOOT_JAVAC_CMD = /usr/local/jdk1.6.0_45/bin/javac  -J-XX:ThreadStackSize=1536 -J-XX:-PrintVMOptions -J-XX:+UnlockDiagnosticVMOptions -J-XX:-LogVMOutput -J-Xmx512m -J-Xms512m -J-XX:PermSize=32m -J-XX:MaxPermSize=160m -encoding ascii -source 6 -target 6 -XDignore.symbol.file=true
  BOOT_JAR_CMD = /usr/local/jdk1.6.0_45/bin/jar
  BOOT_JARSIGNER_CMD = /usr/local/jdk1.6.0_45/bin/jarsigner
  JAVAC_CMD = /usr/local/jdk1.6.0_45/bin/java -XX:-PrintVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-LogVMOutput -Xmx512m -Xms512m -XX:PermSize=32m -XX:MaxPermSize=160m -Xbootclasspath/p:/usr/local/openjdk7/langtools/dist/bootstrap/lib/javac.jar -jar /usr/local/openjdk7/langtools/dist/bootstrap/lib/javac.jar  -g -source 7 -target 7 -encoding ascii -Xbootclasspath:/usr/local/openjdk7/classes 
  JAVAH_CMD = /usr/local/jdk1.6.0_45/bin/java -XX:-PrintVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-LogVMOutput -Xmx512m -Xms512m -XX:PermSize=32m -XX:MaxPermSize=160m -Xbootclasspath/p:/usr/local/openjdk7/langtools/dist/bootstrap/lib/javah.jar:/usr/local/openjdk7/langtools/dist/bootstrap/lib/javac.jar -jar /usr/local/openjdk7/langtools/dist/bootstrap/lib/javah.jar -bootclasspath /usr/local/openjdk7/classes
  JAVADOC_CMD = /usr/local/jdk1.6.0_45/bin/java -XX:-PrintVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-LogVMOutput -Xmx512m -Xms512m -XX:PermSize=32m -XX:MaxPermSize=160m -Xbootclasspath/p:/usr/local/openjdk7/langtools/dist/bootstrap/lib/javadoc.jar:/usr/local/openjdk7/langtools/dist/bootstrap/lib/javac.jar:/usr/local/openjdk7/langtools/dist/bootstrap/lib/doclets.jar -jar /usr/local/openjdk7/langtools/dist/bootstrap/lib/javadoc.jar -bootclasspath /usr/local/openjdk7/classes
 
Build Platform Settings:
  USER = root
  PLATFORM = linux
  ARCH = amd64
  LIBARCH = amd64
  ARCH_FAMILY = amd64
  ARCH_DATA_MODEL = 64
  ARCHPROP = amd64
  ALSA_VERSION = 1.1.0
  OS_VERSION = 2.6.32-696.16.1.el6.x86_64 [requires at least 2.6]
  OS_VARIANT_NAME = RedHat
  OS_VARIANT_VERSION = 
  MB_OF_MEMORY = 1861
 
GNU Make Settings:
  MAKE = make
  MAKE_VER = 3.81 [requires at least 3.81]
  MAKECMDGOALS = sanity
  MAKEFLAGS = w
  SHELL = /bin/sh
 
Target Build Versions:
  JDK_VERSION = 1.7.0
  MILESTONE = internal
  RELEASE = 1.7.0-internal-debug
  FULL_VERSION = 1.7.0-internal-debug-root_2018_05_01_00_10-b00
  BUILD_NUMBER = b00
 
External File/Binary Locations:
  USRJDKINSTANCES_PATH = /opt/java
  BUILD_JDK_IMPORT_PATH = /NOT-SET/re/jdk/1.7.0/promoted/latest/binaries
    ALT_BUILD_JDK_IMPORT_PATH = 
  JDK_IMPORT_PATH = /usr/local/jdk1.6.0_45
    ALT_JDK_IMPORT_PATH = /usr/local/jdk1.6.0_45
  LANGTOOLS_DIST = /usr/local/openjdk7/langtools/dist
    ALT_LANGTOOLS_DIST = /usr/local/openjdk7/langtools/dist
  CORBA_DIST = /usr/local/openjdk7/corba/dist
    ALT_CORBA_DIST = /usr/local/openjdk7/corba/dist
  JAXP_DIST = /usr/local/openjdk7/jaxp/dist
    ALT_JAXP_DIST = /usr/local/openjdk7/jaxp/dist
  JAXWS_DIST = /usr/local/openjdk7/jaxws/dist
    ALT_JAXWS_DIST = /usr/local/openjdk7/jaxws/dist
  HOTSPOT_DOCS_IMPORT_PATH = /usr/local/openjdk7/hotspot/import/docs
    ALT_HOTSPOT_DOCS_IMPORT_PATH = 
  HOTSPOT_IMPORT_PATH = /usr/local/openjdk7/hotspot/import
    ALT_HOTSPOT_IMPORT_PATH = /usr/local/openjdk7/hotspot/import
  HOTSPOT_SERVER_PATH = /usr/local/openjdk7/hotspot/import/jre/lib/amd64/server
    ALT_HOTSPOT_SERVER_PATH = 
  CACERTS_FILE = ./../src/share/lib/security/cacerts
    ALT_CACERTS_FILE = 
  CUPS_HEADERS_PATH = /usr/include
    ALT_CUPS_HEADERS_PATH = 
 
OpenJDK-specific settings:
  FREETYPE_HEADERS_PATH = /usr/local/include/freetype2
    ALT_FREETYPE_HEADERS_PATH = /usr/local/include/freetype2
  FREETYPE_LIB_PATH = /usr/local/lib
    ALT_FREETYPE_LIB_PATH = /usr/local/lib
 
Previous JDK Settings:
  PREVIOUS_RELEASE_PATH = USING-PREVIOUS_RELEASE_IMAGE
    ALT_PREVIOUS_RELEASE_PATH = 
  PREVIOUS_JDK_VERSION = 1.6.0
    ALT_PREVIOUS_JDK_VERSION = 
  PREVIOUS_JDK_FILE = 
    ALT_PREVIOUS_JDK_FILE = 
  PREVIOUS_JRE_FILE = 
    ALT_PREVIOUS_JRE_FILE = 
  PREVIOUS_RELEASE_IMAGE = /usr/local/jdk1.6.0_45
    ALT_PREVIOUS_RELEASE_IMAGE = 


Sanity check passed.
View Code

三、編譯操作

  make 2>&1 | tee $ALT_OUTPUTDIR/build.log  # 編譯,標準錯誤輸出stderr重定向到標準輸出stdout,將編譯過程數據重定向到文件保存。

  若編譯失敗,重新編譯前先執行:make clean

  若編譯成功,提示信息如下:

-- Build times ----------
Target debug_build
Start 2018-04-30 23:30:30
End 2018-04-30 23:45:05
00:01:45 corba
00:03:32 hotspot
00:00:18 jaxp
00:00:24 jaxws
00:08:05 jdk
00:00:31 langtools
00:14:35 TOTAL
-------------------------

四、驗證

  進入/usr/local/openjdk7/bin(即$ALT_OUTPUTDIR/bin)目錄,執行:./java -version

[root@dsp-centos-2-6_9 bin]# ./java -version
openjdk version "1.7.0-internal-debug"
OpenJDK Runtime Environment (build 1.7.0-internal-debug-root_2018_04_30_20_26-b00)
OpenJDK 64-Bit Server VM (build 21.0-b17-jvmg, mixed mode)

五、筆者碰到的問題

  1、java.lang.RuntimeException: time is more than 10 years from present

    修改jdk/src/share/classes/java/util/CurrencyData.properties,將日期改為十年以內,JDK的Bug。

  108行改為: AZ=AZM;2009-12-31-20-00-00;AZN,接下來的377、439、529、555行類似修改保存,重新編譯。

2、/usr/local/openjdk7-debug/jaxws/build/xml_generated/build-drop-jaf_src.xml:96: Redirection detected from https to http. Protocol switch unsafe, not allowed.

  原因是jdk7-jaf-2010_08_19.zip這個包下載不了導致的,解決方案:

將以下三個文件下載後置於OpenJDK解壓後根目錄下的drop目錄下,並在環境變量中加入配置:export ALT_DROPS_DIR=/usr/local/src/openjdk7/drop # 註意目錄Path

jdk7-jaf-2010_08_19.zip,下載地址:https://netix.dl.sourceforge.net/project/jdk7src/input-archives/jdk7-jaf-2010_08_19.zip

jdk7-jaxws2_2_4-b03-2011_05_27.zip,下載地址:http://download.java.net/glassfish/components/jax-ws/openjdk/jdk7/jdk7-jaxws2_2_4-b03-2011_05_27.zip

jaxp145_01.zip,下載地址:http://download.java.net/jaxp/1.4.5/jaxp145_01.zip

六、參考資料:

  《深入理解Java虛擬機:JVM高級特性與最佳實踐》

  [深入理解Java虛擬機]第一章實戰 自己編譯JDK

  ubuntu13 編譯openjdk7的問題並解決

  java-編譯openJdk

  openjdk7 源碼下載

JDK源碼下載

OpenJDK8 Windows編譯

CentOS 6.9 Minimal 編譯OpenJDK 7