Really hand in hand, teach you to compile JDK from scratch

People who use JAVA as a development tool are not unfamiliar with JDK, and use
it almost every day. How many of them have actually compiled JDK?
It is estimated that there are very few
, but we may often encounter some problems, the phenomenon is strange and difficult to define, but if we can do some tricks in JDK, maybe the problem will find a solution

This article is to teach you how to compile a JDK.
Don't just read it, you have to do it yourself

Environmental preparation

The environment I use is Centos7.6 installed on a virtual machine, a pure system, nothing else is installed

view instructions

This is the build description of openjdk8. Later I will take out the screenshots of important parts and explain them briefly
http://hg.openjdk.java.net/jdk8u/jdk8u/raw-file/tip/README-builds.html

According to the instructions,
insert image description here
the command is two steps

bash ./configure
make all

However, some environments
insert image description here
are required to prepare the make version. It must be greater than 3.8.1. Check it yourself. Centos 7.6 and above must meet the need
insert image description here
for a Bootstrap JDK. 7 is required, and 8 cannot be used. You need to add the bin to the path. In this way, there is no need to configure parameters.
Download jdk7,
jdk-7u80-linux-x64.tar.gz in the form of tar
to decompress

tar zxvf jdk-7u80-linux-x64.tar.gz

Write down the location of java
/home/jdk/jdk1.7.0_80/bin
and don’t worry about adding the path, just use a temporary one when compiling later

There is one thing to pay attention to.
If there is JAVA_HOME in the environment variable, remove it
insert image description here

Install components

View components You need to search for
insert image description here
these four components

yum search alsa freetype cups xrender

Select a part to install

yum install alsa-lib alsa-lib-devel freetype-devel freetype cups cups-devel libXrender libXrender-devel

There are many tutorials that say to install ant
, but in fact ant is no longer needed

Ant is no longer used when building the OpenJDK

Installed the correct version

insert image description here
insert image description here
If it is higher than the required version,
centos7.6 can meet the requirements through yum installation

Install other components
insert image description here
So much has been said here, in fact, just install the following

yum groupinstall "Development Tools"
yum install libXi libXi-devel libXtst-devel libXt-devel

download resources

Install mercurial
and then download the resource acquisition project of the source file (similar to a clone command of git, which contains some compiled configuration and resource acquisition files, which are small in size, and then obtain large resources after cloning) and then enter the
directory

yum install mercurial
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u60 jdk8u60
cd jdk8u60
bash ./get_source.sh

Then make some tea and wait

It's easy to fail in this process

If you find such a prompt

WARNING: jaxws exited abnormally (255)
WARNING: jdk exited abnormally (255)

Then execute get_source again.
It will only download a few components that have not been successful, and those that have been successfully downloaded will not be downloaded again.

If you are stuck for a long time and there is no movement, check the bandwidth usage, if there is nothing,
then use

ps aux|grep clone

Kill the python clone process and
get_source will fail. Then
delete the failed directories and
re-execute get_source. This part may be repeated
many times. I have repeated it at least 4 or 5 times. The jaxws and jdk can’t be
downloaded. Finally, I gave up. I can’t download jdk.
Just use the clone address printed on the console to visit the browser, download
the zip package, and unzip it. It
’ll be downloaded soon, and I’ll try again before I'm a fool for trying so many times

execute compile

Before executing, add Bootstrap JDK to the environment variable and go to
many posts on the Internet. In this step, many parameters will be added to configure, such as the path of Bootstrap JDK, the path of alsa, etc. In fact, if the dependencies of alsa are installed through yum, and The bin directory of Bootstrap JDK is added to the path, these do not need to be added

export PATH=/home/jdk/jdk1.7.0_80/bin:$PATH
java -version
bash ./configure --enable-debug --with-target-bits=64

insert image description here
In this way, configure is OK. The ccache that was prompted at the end can be ignored. It is a compilation acceleration thing. It doesn’t matter if you don’t have it. And even if you install it, you will find that you can’t find it in yum.

OK, perform the last step according to the prompts on the official website

make all

After a long wait,
countless warnings appeared in the middle, which made people very anxious.
insert image description here
The compilation is complete.

Is it over here? of course not

We will use it next time

Use your own compiled JDK

Below images is the package
j2sdk-image: JDK
j2re-image: JRE
into JDK to see if the directory structure is very familiar

Let us use the cp command to copy the jdk directory away

cp -r /home/jdk/jdk8u60/build/linux-x86_64-normal-server-fastdebug/images/j2sdk-image /home/jdk/openjdk-8u60-make

check version

/home/jdk/openjdk-8u60-make/bin/java -version

openjdk version “1.8.0-internal-fastdebug”
OpenJDK Runtime Environment (build 1.8.0-internal-fastdebug-root_2020_03_22_10_01-b00)
OpenJDK 64-Bit Server VM (build 25.60-b23-fastdebug, mixed mode)

Next, let's verify
that it is more complicated to find a java implementation. I choose metabase
to download and execute the jar from the official website of metabase
and write a run.sh

export MB_DB_TYPE=mysql
export MB_DB_DBNAME=metabase
export MB_DB_PORT=3306
export MB_DB_USER=账号
export MB_DB_PASS=密码
export MB_DB_HOST=mysql所在机器
/home/jdk/openjdk-8u60-make/bin/java -jar metabase.jar


There is an episode in the middle of the execution . When the metabase finally initializes the database connection, it fails. It prompts

Timed out after 5.0 s

This problem is so annoying that it has not been solved. It doesn’t work if you turn off the firewall, and it doesn’t work if you switch to Oracle’s JDK. It seems that I set something wrong with the network when I created the virtual machine. Let’s put it aside
. According to the executed log, the compiled jdk can run smoothly
insert image description here

Congratulations, so far the compilation is finally successful
and can be packaged and used

Welcome to pay attention to the official account, communicate together and make progress together
Welcome to pay attention to the official account, communicate together and make progress together

Guess you like

Origin blog.csdn.net/cowcomic/article/details/105013251