---
title: "ubuntu 13.04环境编译OpenJDK7"
author: "cduym"
date: "2013-06-22T14:39:12Z"
tags: []
categories: ["编程语言"]
slug: "ubuntu-13-04-openjdk7"
draft: false
lastmod: "2013-06-22T16:47:16Z"
id: 1892416
url: "https://cduym.iteye.com/blog/1892416"
---

##### 从openjdk代码仓库下载源码

 

1. 需要安装Mercurial(hg)版本管理工具。
   
   写道
   
   sudo apt-get install mercurial
2. 安装Mercurial的hclone扩展插件：
   
   写道
   
   hg clone http://bitbucket.org/pmezard/hgforest-crew
3. 将以下内容添加到$HOME/.hgrc文件中，如果没有则自己创建一个：
   
   写道
   
   \[extensions]  
   forest=/home/lichengwu/hgforest-crew/forest.py  
   fetch=
4. 下载jdk7源码：
   
   写道
   
   hg fclone http://hg.openjdk.java.net/jdk7/jdk7  
   cd jdk7/ && sh get\_source.sh

##### 直接下载源码包\[推荐]

写道

http://download.java.net/openjdk/jdk7u4/promoted/b22/

 

### 安装编译必须组件

 

1. 安装gcc、g++、make等
   
   写道
   
   sudo apt-get install build-essential
2. 安装ant 1.7以上
   
   写道
   
   sudo apt-get install ant
3. 安装XRender
   
   写道
   
   sudo apt-get install libxrender-dev  
   sudo apt-get install xorg-dev
4. 安装alsa
   
   写道
   
   sudo apt-get install libasound2-dev
5. Cups
   
   写道
   
   sudo apt-get install libcups2-dev
6. 安装jdk6
   
   写道
   
   #一般ubuntu都自带，没有的话  
   sudo apt-get install openjdk-6-jdk
7. 我是使用的oracle的jdk6，可以编译成功
8. 安装零碎的工具包
   
   写道
   
   sudo apt-get install gawk zip libxtst-dev libxi-dev libxt-dev

### 测试编译脚本：

写道

export LANG=C ALT\_BOOTDIR=/usr/belleing/java  
#disable JAVA\_HOME  
export -n JAVA\_HOME  
export ALT\_JDK\_IMPORT\_PATH=/usr/belleing/java  
make sanity BUILD\_JAXWS=false BUILD\_JAXP =false

 如果出现:Sanity check passed.则表示测试编译通过了，不过也别开心得太早，真正make的时候不保证一定没问题，也许还会缺少某些依赖。

### 编译脚本：

写道

#!/bin/bash  
\# cd jdk source code folder  
cd ~/workspace/openjdk  
\# export ALT\_BOOTDIR  
export LANG=C ALT\_BOOTDIR=/usr/belleing/java  
\# set build profile,first 'chmod +x jdk\_generic\_profile.sh'  
jdk/make/jdk\_generic\_profile.sh  
\# disable JAVA\_HOME  
export -n JAVA\_HOME  
\# export ALT\_JDK\_IMPORT\_PATH  
export ALT\_JDK\_IMPORT\_PATH=/usr/belleing/java  
\# start build  
export ALLOW\_DOWNLOADS=true  
make DEBUG\_NAME=all\_fastdebug  
#make DEBUG\_NAME=all\_fastdebug BUILD\_JAXWS=false BUILD\_JAXP=false

#### 下面就是解决make出现的各种问题和解决方案：

 

**1.ERROR: You do not have access to valid Cups header files.**  
需要安装cpus的dev包

sudo apt-get install libcups2-dev

**2.ERROR: The version of ant being used is older than the required version of '1.7.1'. The version of ant found was ''.**  
这说明没有装ant

sudo apt-get install ant

**3. ERROR: FreeType version 2.3.0 or higher is required.**

安装freetype的dev包

sudo apt-get install libfreetype6-dev

**4. ERROR: You seem to not have installed ALSA 0.9.1 or higher.**  
不需要从ALSA官网下载alsa-dev和alsa-drive， ubuntu提供包的

sudo apt-get install libasound2-dev

**5. ERROR: echo "\*\** This OS is not supported:" 'uname -a'; exit 1;**  
很奇怪的错误，anyway，注释掉hotspot/make/linux/Makefile里面的checkOS

check\_os\_version:  
#ifeq ($(DISABLE\_HOTSPOT\_OS\_VERSION\_CHECK)$(EMPTY\_IF\_NOT\_SUPPORTED),)  
\# $(QUIETLY) &gt;&2 echo "\*\** This OS is not supported:" \`uname -a\`; exit 1;  
#endif

Update: 最好的办法是在make参数后面添加 DISABLE\_HOTSPOT\_OS\_VERSION\_CHECK=OK 即可

 

**6. ERROR: error: "\_\_LEAF" redefined \[-Werror]**

这个是已知的bug(http://hg.openjdk.java.net/hsx/hotspot-comp/hotspot/rev/a6eef545f1a2), 在hopspot下打入该patch(http://hg.openjdk.java.net/hsx/hotspot-comp/hotspot/raw-rev/a6eef545f1a2)即可

**7. ERROR error: converting ‘false’ to pointer type ‘methodOop’ \[-Werror=conversion-null]**

这个的问题是把 false 转换成 NULL的时候出错了  
同样在hotspot下 打入该 patch(http://hg.openjdk.java.net/hsx/hotspot-rt/hotspot/raw-rev/f457154eee8b)

 

**8. ERROR gcc: error: unrecognized command line option '-mimpure-text'**  
这个-mimpure-text是gcc给Solaris的编译选项，所以注释掉即可  
文件在./jdk/make/common/shared/Compiler-gcc.gmk +70

**9. ERROR undefined reference to 'snd\_pcm\_format\_\*\*'**

folow this link [Build openjdk in Ubuntu 11.10](http://comments.gmane.org/gmane.comp.java.openjdk.build.devel/5311)

 

在jdk/make/javax/sound/jsoundalsa/Makefile 里面

![](http://dl2.iteye.com/upload/attachment/0086/0844/8cc640cf-d023-3bb6-bf35-719a380a1f2d.png)  
 

### 编译成功！

![](http://dl2.iteye.com/upload/attachment/0086/0830/6dbfbf3e-2fb0-3797-9930-8af210da2772.png)、

![](http://dl2.iteye.com/upload/attachment/0086/0832/08a41d76-f791-314d-a526-3ab52c4a522e.png)  
 

### 参考资料：

编译：[http://hg.openjdk.java.net/jdk7/build/raw-file/tip/README-builds.html](http://hg.openjdk.java.net/jdk7/build/raw-file/tip/README-builds.html)

http://hadwinzhy.gitcafe.com/blog/2013/01/21/compile-jdk/

http://wendal.net/420.html  
[http://thebye85.iteye.com/blog/1545311](http://thebye85.iteye.com/blog/1545311)

http://rednaxelafx.iteye.com/blog/875957

关于为啥要自己编译jdk：[http://rednaxelafx.iteye.com/blog/1549577](http://rednaxelafx.iteye.com/blog/1549577)