JDK source code reading project building records

Time:2020-7-26

1、 The importance of JDK source code

The importance of JDK source code is self-evident, usually interview, in-depth study and so on are inseparable from the JDK source code. Of course, JDK source code is very good code, we read the JDK source code, is to understand the underlying principles, learn excellent design patterns and ideas. However, JDK source code is also quite difficult to gnaw knowledge points, we must have confidence, not afraid of difficulties, hard to learn, there will be great harvest.

2、 Build JDK source code reading project

1. Create a new project and import JDK source code

First of all, create a common java project. The author will name the project jdksourselarn. You can choose by yourself.
JDK source code reading project building records
JDK source code reading project building records
It is suggested that you create two new directories, one is called source, where JDK source code is placed; the other is called test, where test cases are placed. In this way, the hierarchical structure is relatively clear, and learning from aspects.
JDK source code reading project building records
Now import the JDK source code and find the directory where the JDK is installed src.zip File, that is the source code compressed file. After decompressing it, copy it directly to the source directory of the project.
JDK source code reading project building records
JDK source code reading project building records

2. Create a test case to try debugging

Here, create a related test case in the test directory and try to debug it.

package cn.mambalhb.test;

import java.util.HashMap;
import java.util.Set;

public class HashMapTest {

    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();

        map.put("aaa", "AAA");
        map.put("bbb", "BBB");
        map.put("ccc", "CCC");

        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.println(key + "===>" + map.get(key));
        }
    }

}

After setting breakpoints and clicking debug, you usually need to wait for the project build for a while. At this time, a lot of errors will appear. Next, we will solve them one by one.

3. Solve related problems

(1) Insufficient system resources

The questions are as follows:
JDK source code reading project building records
The prompt shows that the cause of this error is insufficient memory. The solution, of course, is to increase memory.
clickFile–>Settings–>Build,Execution,Deployment–>Compiler, increase the build process heap size (Mbytes) option, the default value is 700, and the author adjusts it to 1200:
JDK source code reading project building records

(2) Package com.sun.tools . javac. * does not exist

The questions are as follows:
JDK source code reading project building records
This is due to the lack of jars for tools.
clickFile–>Project Structure–>Libraries
JDK source code reading project building records
Select the JDK installation directory and find it under the Lib directory tools.jar Click OK to import.
JDK source code reading project building records

(3) Symbol unixtoolkit, fontconfigmanager not found

The questions are as follows:
JDK source code reading project building records
JDK source code reading project building records
This is due to the lack of two Java class files under the windows platform.
These two class files can be found on the openjdk website.
JDK source code reading project building records
Note here to choose their own version of JDK, the author’s jdk1.8.0_ 162。
JDK source code reading project building records
JDK source code reading project building records
JDK source code reading project building records
JDK source code reading project building records
JDK source code reading project building records
JDK source code reading project building records
JDK source code reading project building records
JDK source code reading project building records
JDK source code reading project building records
JDK source code reading project building records
After you find the location of the two class files, copy them into the location of the project as shown in the following figure.
JDK source code reading project building records
When you debug or run the test case, you can see that it has run successfully.
JDK source code reading project building records

3、 Improve project setting

1. Allow debugging in

At this point, if we break the point to step debugging, we can find that ClickStep IntoUnable to enter the source code, because the JDK source code is protected, unable to enter, but clickForce Step IntoCan enter the source code. If you want to pressF7(i.e. ClickStep Into)To enter the source code, you need to make the following settings:
clickFile–>Settings–>Build,Execution,Deployment–>Debugger–>Stepping, cancel the checkDo not step into the classes
JDK source code reading project building records

2. Edit source file

If we want to record or comment on the source code file, we will find that the source code file is read-only and cannot be edited. At this time, the source code file associated with the project is not the file just copied into the project, but the source code file under the JDK installation directory. Re set the source code file associated with the project to the source code file under the project.
clickFile–>Project Structure–>SDKs
JDK source code reading project building records
Here, delete the associated source code path under the original JDK installation directory and add the source code path just copied to the project directory.
JDK source code reading project building records
At this time, when debugging into the source code, you can find that the small lock mark on the file has disappeared, and the source file can also be annotated and recorded.
JDK source code reading project building records

4、 Summary

So far, the construction of JDK source code reading project has been completed. When learning the source code, you can’t just look at it. You must start debugging. In the process of debugging, you can read the source code, make records, and write notes. Turn input into output and stick to it.