Compile and install OpenJDK8 from source code under Ubuntu 18.04.1

Compile a JDK yourself to increase interest in JVM. This article is divided into three parts to describe the process of compiling OpenJDK, which are the preparations before compilation, the construction of the compilation environment, and the compilation. In these three parts, the pits are explained together.

  1. Preparation before compilation

1.1 Install Linux environment

Compiling OpenJDK, Windows environment is more complicated than Linux environment, so choose to install Linux environment. You can install the virtual machine under Windows by yourself, or you can install the dual system directly, and a disk is dedicated to install Linux. I chose the latter and install the latest version of Ubuntu 18.04.1 LTS. The process of building a dual system will not be repeated here. , There are a lot of materials on the Internet for reference. My computer is ThinkPad-E480. After the installation is complete, I can’t connect to WIFI because of the huge pit, which made me spend a night to solve this problem, and attach the link to solve the problem:

https://askubuntu.com/questions/1070593/lenovo-thinkpad-e480-no-wifi-adaptor-found-in-ubuntu-18-04

1.2 Download the OpenJDK source code

The original plan was to follow Teacher Zhou’s book step by step, so the plan was to compile OpenJDK7. The BootStrapJDK corresponding to OpenJDK7 is OpenJDK6. But OpenJDK6 is difficult to find resources on Ubuntu 18.04.1 LTS, so I gave up this idea and changed it to Compile OpenJDK8, BootStrapJDK is OpenJDK7. Practice has proved that this operation is also a pit all the way, which will be described later.

After determining the idea, the next step is to download the source code of OpenJDK8, there are two ways:

The first is Mercurial. The advantage is that it is very simple to operate and does not need to decompress the file package. The disadvantage is that it takes a longer time. In fact, I will use this method in the end, which is expected to take about half an hour. Mercurial is also a version management tool. You can imagine tools like SVN and Git. The command to download the code is as follows:

hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev
cd jdk8u-dev
sh get_source.sh

The second is the manual method. To put it bluntly, download the source code package by yourself and decompress it. The advantage is that it takes a short time, but relatively speaking, if you can’t find resources, you can only download the old version of the source code. Find the source code under this link http://jdk.java.net/java-se-ri/8. This version is the 2015 version. Three years have passed since now. Such code is actually in the later compilation process. If you encounter some problems, you cannot judge whether it is a Linux problem or OpenJDK8 code problem.

So far, there is a Linux operating system and the OpenJDK source code that needs to be compiled. The next step is to think about how to build a compilation environment.

Second, build the compilation environment

After studying for so many years, everyone should have a certain learning method. A very important learning method for IT is to read the DEMO or README of the information after you get the information. Similarly, there is such a file in the OpenJDK source code directory called README-builds.html.

This file basically runs through the operation process of our article. First, let's take a look at Introduction:

Compared with the construction of OpenJDK7, Ant is no longer needed, and the environment variables of ALT_* are no longer supported. For the compilation process of OpenJDK7, please refer to Mr. Zhou's book and other information on the Internet.

The second part of the file is to download the source code. The code download link has been reflected in 1.2 of this article, so I won’t repeat it here.

The third part is Building, where the software and hardware requirements of each operating system environment are stated, and the boot JDK of OpenJDK8 is clearly required to be JDK 7.

2.1 Install boot JDK

The Specific Developer Build Environments section of the file actually agreed on how to install the boot JDK, the command is as follows (aptitude should be changed to apt-get in Ubuntu 18.04.1 LTS):

sudo aptitude build-dep openjdk-7 
sudo aptitude install openjdk-7-jdk

In actual execution, the above command is also unsuccessful, prompting that there are no installation candidates. This is one of the pits mentioned in 1.2 of this article. How to solve it? Please see https://askubuntu.com/questions/761127/how-do-i-install-openjdk-7-on-ubuntu-16-04-or-higher, the ppa method inside has expired, you can only press To operate according to the scheme described by MDMower, I chose Manual Installation and finally successfully installed the boot JDK. The results are as follows:

linuxidc@linuxidc:~$ java -version
java version "1.7.0_161"
OpenJDK Runtime Environment (IcedTea 2.6.12) (7u161-2.6.12-1)
OpenJDK 64-Bit Server VM (build 24.161-b01, mixed mode)

2.2 Dependency check

In fact, if you follow the process of README-builds.html, the dependency check is performed before installing the boot JDK. Even if the boot JDK is not installed first, if you check directly through bash ./configure, the first prompt in this step is also the installation boot JDK, the prompt is as follows:

configure: Could not find a valid Boot JDK. You might be able to fix this by running 'sudo apt-get install openjdk-7-jdk'.
configure: This might be fixed by explicitely setting –with-boot-jdk

After completing 2.1 of this article, the next step is to recursively execute bash ./configure to check whether all the dependencies of the compilation environment are installed. Until you see this result:

====================================================
A new configuration has been successfully created in
/home/linuxidc/jdk8u-dev/build/linux-x86_64-normal-server-release
using default settings.

Configuration summary:

Tools summary:

Build performance summary:

Here is a supplementary explanation. During the recursive execution of the dependency check, the libx11-dev may be prompted. Ubuntu 18.04.1 LTS prompts the installation project as follows:

sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev

The X in libX11-dev is uppercase, and you should be prompted that this dependency cannot be found. At this time, you need to change the uppercase X to lowercase x, libx11-dev, and you can find the dependency.
At this step, the compilation environment for OpenJDK 8 is ready, and the next step is to compile OpenJDK 8.

Three, compile

The compiled code is very simple, just make all directly, of course, you can also compile with parameters to make according to README-builds.html, the instructions are as follows:

Make Target Description
empty build everything but no images
all build everything including images
all-conf build all configurations
images create complete j2sdk and j2re images
install install the generated images locally, typically in /usr/local
clean remove all files generated by make, but not those generated by configure
dist-clean remove all files generated by both and configure (basically killing the configuration)
help give some help on using make, including some interesting make targets

There are several precautions before compilation. These precautions are also reflected in the file README-builds.html:

To set the language option, you can execute echo $LANG first and check the output. If it is not C, execute export LANG=C;

To set the PATH, you can execute echo PATH first and check the output. If there is no bootJDK, execute exportPATH="/usr/lib/jvm/java−7−openjdk−amd64/bin:

To check JAVA_HOME, execute echo $JAVA_HOME first, check the output, if there is a value, you need to unset JAVA_HOME;

After these three-step checks have passed, you can execute the make command. If everything goes well, you can see this compilation result:

Finished docs (build time 00:01:46)


Finished building OpenJDK for target 'all'

Seeing such a result indicates that the compilation was successful. You can execute ./java -version in the bin directory under multiple directories to verify.

linuxidc@linuxidc:~/jdk8u-dev/build/linux-x86_64-normal-server-release/jdk/bin$ ./java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-linuxidc_2018_09_23_16_59-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00, mixed mode)

linuxidc@linuxidc:~/jdk8u-dev/build/linux-x86_64-normal-server-release/images/j2sdk-image/bin$ ./java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-linuxidc_2018_09_23_16_59-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00, mixed mode)

In fact, my compilation process was not smooth, and I encountered two main problems:

1、 Compile kernel version problem

There are two ways to get the source code mentioned in 1.2 of this article. In fact, I used method two at the beginning. I downloaded the 2015 openjdk-8u40, which is stated in the /hotspot/make/linux/Makefile file in the source package. The SUPPORTED_OS_VERSION does not support the 4.X kernel, so the compiler reports the following screenshot error:

Because the kernel of Ubuntu 18.04.1 LTS is 4.15.0-34-generic, if you want to continue compiling, you need to add 4% after the SUPPORTED_OS_VERSION line of the Makefile.

2、- Werror=deprecated-declarations problem

After I solved problem 1 and continued to compile, I encountered many magical problems later, and it was difficult to find relevant information to solve the problem. So I can only logically reason that OpenJDK8 has been updated and developed, and Ubuntu has also been updated and developed. The two are updated simultaneously. The latest files should be used to compile the problems. And guess there are more information available. , But now I use the 2015 openjdk-8u40, and Ubuntu is the latest, so no one will repair it if there is a problem (in fact, many of our application systems are the same reason, they are in disrepair for a long time, and no one uses the function If there is a problem, it may not be fixed). At this time, I decided to switch to the latest OpenJDK8, downloaded the latest code through Mercurial, and then compiled it on Ubuntu 18.04.1 LTS. The compilation process encountered a problem, and the error was reported as follows:

os_linux.inline.hpp:127:18: error: 'int readdir_r(DIR*, dirent*, dirent**)' is deprecated [-Werror=deprecated-declarations]

According to online information, it is because when glibc >= 2.24, the method readdir_r is deprecated and is not supported. Through the getconf GNU_LIBC_VERSION check, it is found that the Ubuntu 18.04.1 LTS version is glibc 2.27, and many people have reported bugs in OpenJDK. The link https://bugs.openjdk.java.net/browse/JDK-8179887, 6/7/8/9 does not intend to fix this problem, it will fix this BUG in 11, so currently it can only be solved by other means , The solution is as follows:

Find WARNINGS_ARE_ERRORS = -Werro in the ./hotspot/make/linux/makefiles/gcc.make file, comment this paragraph or change it to WARNINGS_ARE_ERRORS = -Wno-all. Compiling again will ignore the warning until the compilation is complete.

Recommended Posts

Compile and install OpenJDK8 from source code under Ubuntu 18.04.1
Ubuntu 18.04 source code compile and install ffmpeg4.1
How to compile and install xdebug from source code in Ubuntu environment
Compile and install QEMU under Ubuntu
CentOS7.4 source code compile and install MySQL8.0
Install MySQL 8.x from source code under CentOS7
Ubuntu 16.04 compile and install PHP 7.2
ubuntu18.04 compile and install python3.8
Compile and install LAMP under Centos 5.2
Install and deploy Gerrit under Ubuntu
Install nodejs and npm under Ubuntu 16.04
Compile FFMPEG source code under CentOS7
Compile and install libmodbus library under CentOS7
Download OpenJDK11 source code in Ubuntu environment
Ubuntu 18.04 install Odoo14 tutorial through source code
Edit OpenJDK11 source code in Ubuntu environment
How to install openssh from centos 7 source code
Install apache+PHP under Ubuntu
Install node.js under Ubuntu
Install python3.6 under Ubuntu 16.04
Install mysql under Ubuntu 16.04
Install Caffe under Ubuntu 14.04
[python] python2 and python3 under ubuntu
2018-09-11 Install arduino under Ubuntu
Install ROS under ROS Ubuntu 18.04[2]
Install MySQL under Ubuntu
Install Yarm-PM2 under Ubuntu
Install win7 and archlinux on hard disk under ubuntu
Cpp web (1) Install and use Crow service under Ubuntu
Compile and install Lnmp shell script under Linux centos
Centos compile and install Git
ubuntu install and configure GitLab
Install server-side Shadowsocks under Ubuntu 16.04
Centos compile JDK8 source code
Centos7 compile and install ntp-4.2.8p11
CentOS 6.9 compile and install python
CentOS 6 compile and install python 3
Ubuntu16.04 install SVN and configuration
Ubuntu16.04 install and uninstall VMware-Workstation 14
Install rgl package under Ubuntu
Compile and install the open source EDA tool-Surelog on CentOS8
[Daily] Install php pdo extension and import database under ubuntu
How to compile and install PHP and Nginx in Ubuntu environment
Install and configure keepalived under CentOS 5.9
Install and configure MySQL on Ubuntu
How to compile ijkplayer-android under ubuntu
Install 3 single-cell R packages under Ubuntu
Install and configure Docker in Ubuntu
CentOS7 compile and install L(A|N)MP environment
Android source code compilation (ubuntu16.04 64-bit)
Install MySQL under Ubuntu 18.04 (graphic tutorial)
Install JDK1.8 original under Ubuntu Kylin
Redis installation under ubuntu and windows
Small tool: install screenshot tool under ubuntu
Centos7 uninstall openJdk, and install JDK1.8
Install Python3 and ansible under CentOS8
Install and use docker under CentOS 6.8
Install Python3 and Py under CentOS7
Coexistence of CUDA8.0 and CUDA9.0 under Ubuntu 16.04
Linux CentOS6 compile and install Pyt
How to install Audacious under Ubuntu