It’s 2039. Can’t you compile JDK source code? It’s time to read this article~~

Time:2021-2-25

1、 Overview

What is your most unforgettable experience? If let me answer this question, then I think the first time I learned java was one of my most unforgettable experiences. I remember it was a night of thunder and lightning, wind and rain… Doodle doodle, ha ha, I started to write the first hello with a video World program, it was a happy time at that time ~ ~; as the saying goes, the past can’t be recalled. I remember that when I first started learning Java, there was no one to guide me, or “martyrs” to guide me. As a result, I ran into a wall in that dark learning process and didn’t form a good programming habit. Especially when I was learning the basics, I never thought about looking at the source code of an API, For a long time later, they all became “API boy”. When they grow up, they are responsible for the bravado they once boasted about when they were young, so they can only make up for it.

To get to the point, this article manually compiled JDK source code, led you to familiarity with the whole compilation process, old fellow can be compiled according to the results of the project in their own introduction, custom source code, and so on, bullshit less, it is finished.

2、 Environmental preparation

2.1 boot JDK

Before compiling JDK source code, you must install JDK on your compiler machine, which is officially called boot JDK. For example, if you want to compile the source code of jdk8, then you must include JDK7 or an updated version on your compiler machine. As for why you want to do such a thing, it’s not the focus of this article. I’ll share it later.

My understanding is the process of “laying hens, laying hens”. The compilation process needs the support of JDK tools

2.2 compiler and dependency

When compiling JDK source code, it is best to use UNIX kernel system. After all, windows is not the mainstream operating system environment for compiling source code. In this paper, I mainly use cenos7. X for compilation demonstration. Before compiling, I need to install the compilation environment and some necessary toolkits

Yum install GCC + + – y # C or C + + compiler
Yum install libxtst devel libxt devel libxrender devel – y # compile related dependencies
yum install cups-devel -y
yum install freetype-devel -y
yum install alsa-lib-devel -y

Note: in fact, if you don’t install these software, you will also fail in the configure phase of compilation. You will be prompted which packages are missing, and the installation command will be attached. I will find out the missing packages on my machine step by step. As for the screenshots of errors caused by these missing dependencies, I will not arrange them one by one because of the length. This is also to give readers a little suspense, Guide readers to verify by themselves.

3、 JDK source code download

visitOfficial website address, Download JDK source code package:

It's 2039. Can't you compile JDK source code? It's time to read this article~~

It's 2039. Can't you compile JDK source code? It's time to read this article~~

Note: this demonstration uses the source code of jdk8. In addition, you can also copy the link address to download the source code, and download it on the Linux host using the WGet command

4、 Compilation process

4.1 upload and unzip the source code

[ [email protected] ~]# cp openjdk-8u41-src-b04-14_ jan_ 2020. Zip / usr / local / # copy the source code package to the installation directory
[ [email protected] ~]#CD / usr / local / # enter the corresponding directory
[ [email protected] local]# unzip openjdk-8u41-src-b04-14_ jan_ 2020. Zip to the current directory
[ [email protected] Local] # ln – s openjdk8source. / openjdk # add soft link

4.2 configure

[ [email protected] ~]#CD / usr / local / #
[ [email protected] Local] # CD openjdk8source / # enter the directory after decompression
[ [email protected] Openjdk8source] # chown U + X configure # add the execution permission of confirm file
[ [email protected] Openjdk8source] #. / configure # run configure

Note: the configuration file has no execution permission by default and needs to be granted manually.

It's 2039. Can't you compile JDK source code? It's time to read this article~~

The reason for success is lack of dependency. Every time you fail to configure, you will be prompted what is missing and what is the installation command. As shown in the figure below, how to get the missing software package on the machine mentioned in the summary of article 2.2 is exactly found in this step.

It's 2039. Can't you compile JDK source code? It's time to read this article~~

Configuration will be executed for a while, and you need to wait patiently. After the final configuration is completed, you will be prompted with the following information

It's 2039. Can't you compile JDK source code? It's time to read this article~~

4.3.make all

After the successful execution of 4.2, the compiler will be followed by running the make all command directly. I have done it all at once. The compilation takes a few minutes, but no other errors have been reported.

It's 2039. Can't you compile JDK source code? It's time to read this article~~

Note: about the configure and make process, if you have Linux operation and maintenance experience or C / C + + development experience, you should know that the configure process generates the compiled configuration file makefile through the template. The make process actually compiles according to the makefile file file and outputs the compilation results to the specified file. The configure process can also manually specify the configuration parameters. For Lin How to compile the source code package under UX, I will output in the next article.

5、 Verification

Do you remember the last prompt path after configure (if you don’t remember to see Figure 2 in Figure 4.2), yes, the storage path of the officially compiled file:

/usr/local/openjdk/build/linux-x86_64-normal-server-release

It's 2039. Can't you compile JDK source code? It's time to read this article~~

It's 2039. Can't you compile JDK source code? It's time to read this article~~

JDK and JRE compiled file directory:

It's 2039. Can't you compile JDK source code? It's time to read this article~~

See here, it means that you have seen the whole compilation process, and then introduce the results in your own project.

6、 Using compiled JDK

Use the compiled JDK to run the first hello world program

[[email protected] ~]# vim Test.java
public class Test{
public static void main(String args[]){
System.out.println(“hello world!”);
}
}

Switch JDK environment variables and run the program

1. Switch environment variables to JDK compiled by yourself
vim /etc/profile.d/jdk.sh
Add content
export JAVA_HOME=/usr/local/openjdk/build/linux-x86_64-normal-server-release/images/j2sdk-image
export PATH=$JAVA_HOME/bin:$PATH
take effect
[ [email protected] ~]#Source / etc / profile # let shell program reread environment variable configuration

2. Check whether the environment variable is effective
[[email protected] ~]# java -version
openjdk version “1.8.0-internal”
OpenJDK Runtime Environment (build 1.8.0-internal-root_ 2020_ 08_ 08_ 17_ This sentence indicates that the environment variable has been switched over
OpenJDK 64-Bit Server VM (build 25.40-b25, mixed mode)

2. Compilation:
[[email protected] ~]# javac ./Test.java

3. Operation:
[ [email protected] ~]#Java test # run without file suffix
Hello world

Customize JDK

If you need to customize some JDK classes or methods (add, delete and modify), you need to modify them in the decompressed source directory, which is located at:

/usr/local/openjdk/jdk/src/share/

After modification, you need to recompile the JDK source code, and finally get the compiled JDK source code to have your extended functions ~ ~ Yes, I know how to write my resume, right? Proficient in JDK secondary development, ha ha, a joke~

7、 Summary

Speaking of this, ha, a friend will ask, brother, I usually work in the window when I code. Can the things you compile for me with the UNIX kernel system run on the window? The answer is definitely No. remember why Java can cross platform? Yes, it’s not that Java can cross platforms, but that the implementation of JVM is based on different platforms, which means that JDK compiled by different platforms can only run on corresponding platforms, and JVM translates corresponding machine code according to different platforms, so JVM is the cornerstone of Java’s cross platform ability; so coders of window platform are embarrassed, and they can only try to compile on window themselves, after all, they have no idea It’s up there~~

All right, let’s see you next time. Bye~