I am kite, the public account “ancient time kite”, a not only technology public account, I have been in the app community for many years, mainly Java, Python, React also play 6 slash developer. The Spring Cloud series is complete, and you can check out the full series on my Github. You can also reply “PDF” in the public account to get the full PDF version of my elaborate tutorial.

Every day you write Java code that needs JDK support and runs on the JVM, you wonder what the JDK looks like. Curious, compile and implement your own JDK.

This compilation environment macOS 10.12, compilation is JDK 11 version.

Installation of its 11

To compile the OpenJDK, you need to install either OpenJDK 10 or OpenJDK 11 on the machine as the Boot JDK. Install openJDK 11 to compile, you can go to the adoptopenJDK website to download.

Installation in PKG format

Enter the page https://adoptopenjdk.net/index.html?variant=openjdk11&jvmVariant=hotspot download directly download, then double-click the can complete the installation.

Installation in tar.gz format

1, enter the page https://adoptopenjdk.net/installation.html?variant=openjdk11&jvmVariant=hotspot#x64_mac-jdk to download the tar. Gz packages

2, decompression

The tar - xf OpenJDK11U - jdk_x64_mac_hotspot_11. 0.5 _10. Tar. GzCopy the code

After decompression is a macOS package, you can right-click -> Show package contents to view the files inside.

3. Add the environment variable PATH. If you’re using another VERSION of the JDK for development, skip this step.

The export PATH = $PWD/JDK - 11.0.5 + 10 / Contents/Home/bin: $PATHCopy the code

4, macOS JDK default directory in the/Library/Java/JavaVirtualMachines, put the content of the step 2 decompression in this directory, then the process of compilation in the directory lookup JDK 10 or 11 JDK.

Here is my local directory structure, there are three versions 7, 8, 11, the development of the default use of 8.


Install xcode

What we actually need is not Xcode, but the LLVM compiler command clang. Of course you can install LLVM separately, but this article is for Java developers, and installing Xcode is the easiest version.

I installed Xcode 8.1 locally a long time ago, it compiles without any problems, if you use a newer version, you should not have any problems, you can try it yourself.

Begin to compile

Download the OpenJDK 11 source code

Its source on the web site, http://hg.openjdk.java.net/, we want to download JDK11 directory at http://hg.openjdk.java.net/jdk-updates/jdk11u/.

After entering the page, click browse on the left and select a compressed format to download.


You can also clone to a local location using hg, which requires mercurial installation and is not recommended if you have a poor or unstable Internet connection.

hg clone https://hg.openjdk.java.net/jdk/jdk11/
Copy the code

2, decompress the source package

Decompress the package you just downloaded to a full English directory, do not use Chinese, to reduce the trouble caused by compilation.

3, the configure

Go to the directory decompressed in the previous step and run the following command.

sh configure --with-target-bits=64 --enable-ccache --with-jvm-variants=server  --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log
Copy the code

Execute this command is the premise of I have already put its 11 in/Library/Java/JavaVirtualMachines directory.

If you don’t want to put it in this directory, it is ok, you need to specify additional parameters

- with the boot - JDK = its directoryCopy the code

If the following output appears, this step is normal.


4, make

Use the make command to compile.

make
Copy the code

The first compilation will be slow. Mine is the MacBook Pro i5 8G, which took about 10 minutes to compile. If the following output is displayed, the compilation is successful.

Finished building target 'default (exploded-image)' in configuration 'macosx-x86_64-normal-server-slowdebug'
Copy the code

Slowdebug: slowdebug (slowdebug) : slowdebug (slowdebug) : slowdebug (slowdebug) : slowdebug (slowdebug) : slowdebug (slowdebug)

IDEA uses a compiled JDK for configuration

1. Open IDEA and find File->Project Structure.


2. Add a JDK


3. Select the JDK compiled from the source code above


4. Specify the JDK when you finally start the project.

Use CLion debugging

1. Open CLion, import the project, and select the SRC directory where you downloaded the source code.


Configure Debug Configurations, select Executable as the compiled Java Executable in the bin directory, and remove the Build Settings.

Program Arguments is set to -version, as well as others. Setting to -version means Java -version.


3. Finally, make a breakpoint in the source code, such as jni. CPP or thread. CPP, and click Debug.

Build your own JDK

It’s not that easy to build your own JDK, and it’s just not there. Here’s an idea. Sometimes, for example, we debug Java code and find ourselves in the JVM layer, in which case we can’t follow it. We don’t know how the variables change once we get to the JVM layer. At this point, we find the JVM corresponding code and change it a little bit, such as printf output parameter values can be clearly seen.

Modifying JDK code

I found the JavaMain(void * _args) method of the java.c file in CLion, added a line of print code, and just about implemented my OWN JDK (smiling face).

The first step in a long journey is not important, leaving footprints is the key.

printf("Ancient Kite JDK \n");
Copy the code

Recompile the modified source code

After making the changes, go to the root of the source directory on the terminal and execute the make command.

Because you’ve compiled it before, executing make again is incremental compilation, so it’s fast.

All right, it’s time for the magic

We had previously added the compiled JDK to IDEA and assigned it to a project. For testing only, the code is as follows.

public static void main(String[] args){
    System.out.println("hello jvm");
}
Copy the code

When we run this project, if it’s a normal JDK, we’ll print Hello JVM on the console, right?

However, this is not the normal JDK specified, is the JDK I have been supported.

When I run it, the output looks like this. See, that line of code that I just added is working.


The kite says

There aren’t many people who can actually do JDK custom development, and I don’t have it at all. But every Java developer has compiled the JDK source code, and it’s worth looking it over. After all, every day we write code that needs JDK support and runs on the JVM, and we wonder what it will look like.

In addition, it may provide a way for us to solve problems on a daily basis. Some people say that the best teacher is the search engine. In most cases, this is true, but sometimes the best way is to look at the source code.

Why do some people solve problems quickly, while some seemingly unsolvable problems can be quickly solved in the hands of the bull? Sometimes the dimension of problem solving is not the same, others are in the three-dimensional world, but you have been in the two-dimensional plane of the circle, for example, encountered program problems, can only analyze the Java level of the problem that is two-dimensional, into the JDK, JVM source code that is into the three-dimensional. The higher the dimension, the different Angle, the more possibilities and ways to solve problems. This is like the three-body advanced civilization using two-way foil to strike, completely under one volume.

Go ahead and build your own JDK.

Strong man wait, first give a praise bar, always white piao, the body can not bear!

I am kite, public id “ancient Time kite”, I have been working in the application circles for many years, mainly Java, Python, React also play very 6 slash developer. Can add my friends in the public account, into the group of small partners exchange learning, a lot of dachang students are also in the group yo.

Technical exchange can also add group or directly add my wechat.