This article is too hardcore and is recommended for those with Java experience.
1 the introduction
Since last week, I have been reading Zhou Zhiming’s “Deep Understanding of the Java Virtual Machine: Advanced JVM Features and Best Practices (3rd edition)”. I have read the 2nd edition many years ago, and it is definitely a classic work in the field of JVM in China. It is worth reading several times.
At the beginning of the book, I introduced how to compile the JDK by myself. I had skipped the previous book and had never operated it by myself. When I read here last week, I suddenly had the idea of practicing it.
First of all, I chose the OpenJDK this time instead of the OracleJDK. The OracleJDK will have some independent commercial features, and these commercial features are not open source, so I chose the OpenJDK.
Before I started compiling, I also looked at a lot of things on the Internet, and all of them suggested that I should build on a unix-based system, namely Linux or MacOS, but I was a hard nut to crack and couldn’t do it from 9am to 12am on Windows10 last week, so I capitulated.
Due to lack of money and a lack of a Mac, he turned to Linux.
Generally, the available distribution is either CentOS or Ubuntu. Later, I happened to see someone recommend deepin, the domestic Linux desktop version, and then I cut 128GB space on my computer to make a dual system. Just try compiling OpenJDK while trying out this operating system.
2 its source
First I used Deepin20 Community Edition.
The target is OpenJDK14, the latest JDK release.
The OpenJDK source code is managed using the Mercurial code version Management tool and can be obtained directly from Repository:
hg clone https://hg.openjdk.java.net/jdk/jdk14
Copy the code
But let’s do it this way, it is too slow in China, without some network related Settings, it will take a morning to clone the whole thing, so it is not recommended to do this, you can directly access the code repository, use zip download directly to download the code package.
Direct access to the hg.openjdk.java.net/jdk/jdk14/, click on the left side of the browse, download and then click on the button on the left side of the zip, I am here to download the address is:
https://hg.openjdk.java.net/jdk/jdk14/archive/6c954123ee8d.zip
Copy the code
I don’t know if this download address is available for a long time, so let’s leave it.
Once the zip was downloaded, I was done with the first step. I got the OpenJDK14 source code package. In the source code package, there was a doc directory with a building.html, which actually compiled the instruction manual, Go to this manual for solutions when compiling problems occur.
3 Environment Preparation
The first thing you need to do is install JDK13 locally. If you want to build JDK14, you need to install the JDK13 locally.
http://jdk.java.net/java-se-ri/13
Copy the code
The deepin system environment variables are in the user directory. Bashrc, because THIS is the desktop version of the system, directly use VSCode in the last configuration of the file JDK13 environment variables:
Export JAVA_HOME=/home/ username /Java/ jdK-13 export CLASSPATH=${JAVA_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATHCopy the code
Then refresh the configuration file in the terminal command line:
Source /home/username/.bashrcCopy the code
Enter the classic Java -version command to view the version information:
openjdk version "13" 2019-09-17
OpenJDK Runtime Environment (build 13+33)
OpenJDK 64-Bit Server VM (build 13+33, mixed mode, sharing)
Copy the code
Then I started installing various environments in preparation for the build. Since there was a lot of real software to install, I put the commands in the following:
sudo apt-get install build-essentail -y
sudo apt-get install libfreetype6-dev -y
sudo apt-get install libcups2-dev -y
sudo apt-get install libfontconfig1-dev -y
sudo apt-get install libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev -y
sudo apt-get install libasound2-dev -y
sudo apt-get install libffi-dev -y
sudo apt-get install autoconf -y
Copy the code
If the current user is root, there is no need to add the first sudo. If the user is not root, please remember to add sudo. I used to open terminal to switch the user to root first.
After executing the above command, please check the GCC version, because the documentation of the downloaded source package clearly indicates the GCC version limits. Failure to do so will most likely result in a compile failure.
#The input
gcc --version
#The outputGCC (Uos 8.3.0.3-3+ Rebuild) 8.3.0 Copyright (C) 2018 Free Software Foundation, Inc. This is Free Software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.Copy the code
OpenJDK14 requires the GCC version to be 8.3.0, please note that otherwise the compile check will pass, but the packaged image will fail (as I did in my previous experiments on Ubuntu virtual machines).
We are required to use GCC between 4.8 and 8.3. The closer we get to 8.3, the closer we get to success.
At this point, our compilation environment is ready to start compiling.
4 During compilation
The first step is to use the configure command to check the build.
The configure command is responsible for checking dependencies, configuring parameters, and constructing the output directory structure. If a required tool chain or dependency is missing during compilation, a clear prompt will be displayed after the command is executed, and the dependency installation command will be given.
I use the following command for a compile check:
bash configure --enable-debug --with-jvm-variants=server
Copy the code
The meaning of this command is to compile the FastDebug version of the HotSpot VIRTUAL machine with only Server mode.
The configure command has a number of other parameters, which I will not copy from the book. If you want to know, please read it or use bash configure –help.
Next we enter the compile check command above. If the check passes, the following message will be printed:
Some information is output here, including debugging level, Java virtual machine mode, features, compiler version used, and configuration summary information.
Then came the exciting moment, the last command was actually quite simple, make images, and after executing this command, we actually started the OpenJDK compilation process.
The next thing you can do is have a cup of tea…
Have another cup of tea…
Have another cup of tea…
.
Until after the NTH cup of tea, it compiled.
I used a desktop computer with 16G ram and CPU Core I7-8700 physical 6 core virtual 12 core. The first full compilation took about 8 minutes.
If you compile it in full, but you change it and do incremental compilation, this is much faster, and in the book it says you can compile it in 10 seconds. I’ve never tried that. I have no say.
OpenJDK14: build/linux-x86_64-server-fastdebug/ JDK: OpenJDK14: build/linux-x86_64-server-fastdebug/ JDK:
#The input
java -version
#The output
openjdk version "14-internal" 2020-03-17
OpenJDK Runtime Environment (fastdebug build 14-internal+0-adhoc.weishiyao.jdk14-6c954123ee8d)
OpenJDK 64-Bit Server VM (fastdebug build 14-internal+0-adhoc.weishiyao.jdk14-6c954123ee8d, mixed mode)
Copy the code
As you can see, the JDK in the system has become the version I compiled just now, even the name has become my machine name, and then I can use the JDK compiled by myself, or quite a sense of achievement.