Write Java so long to compile a JDK children play it

Every day you write Java code needed to support the JDK, to be run on the JVM, do not you curious JDK look like it. Curious, to compile and implement its own JDK it.

The build environment macOS 10.12, the compiled version is JDK 11.

Install OpenJDK 11

OpenJDK OpenJDK compilation need to be installed on the machine 10 or OpenJDK 11, as Boot JDK.
OpenJDK 11 compiler needs to install, can go to the website to download adoptopenjdk.

pkg install format

Enter the page https://adoptopenjdk.net/index.html?variant=openjdk11&jvmVariant=hotspotDirect Download, then double-click on it to complete the installation.

tar.gz format Installation

1, enter the page https://adoptopenjdk.net/installation.html?variant=openjdk11&jvmVariant=hotspot#x64_mac-jdkdownload tar.gz package

2, extract

tar -xf OpenJDK11U-jdk_x64_mac_hotspot_11.0.5_10.tar.gz

After decompression is a macOS package by right click -> Show Package Contents view inside the file.

3. Add the PATH environment variable, of course, if you are using a different version of the JDK as a development, please ignore this step.

export PATH=$PWD/jdk-11.0.5+10/Contents/Home/bin:$PATH

4, macOS JDK default directory /Library/Java/JavaVirtualMachines, to extract the contents of the second step into this directory, and then compilation process looks for JDK 10 or JDK 11 in this catalog.

Here is my local directory structure, 7, 8, 11 three versions, developed when using 8 or default.

Install xcode

In fact we need is not xcode, but LLVM compiler command clang. Of course, you can install LLVM alone, but limited to this post was written for Java developers, it is the easiest to install xcode version.

I was a long time ago a local installation of xcode 8.1, compile it is no problem, if you are using a newer version, does not appear to be any problems, you can try it yourself.

Start compiling

1, download the OpenJDK 11 Source

OpenJDK source code on the Web site http://hg.openjdk.java.net/ , we want to download JDK11 directory http://hg.openjdk.java.net/jdk-updates/jdk11u/ .

After entering the page, first click browse on the left, and then select a compression format download.

Of course, you can also use the command hg clone to local, using hg need to install mercurial, unstable or if the speed is not good, do not recommend using this approach.

hg clone https://hg.openjdk.java.net/jdk/jdk11/

2, extract the source packet

Will you just downloaded archive decompression, please unzip it into a directory in English, not Chinese, brought trouble to reduce compilation time.

3、configure

Enter the directory after decompression step, execute 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

Premise implementation of this order is that I have OpenJDK 11 into a /Library/Java/JavaVirtualMachinesdirectory.

If you do not put this directory, is also possible, you need to specify additional parameters

--with-boot-jdk=OpenJDK 目录

If the following output appears, indicating that this step is normal.

4、make

Officially began compiled using the make command.

make

The first compilation is slower, my MacBook Pro is that section i5 8G, about 10 minutes to compile it. When the output appears as follows, indicating successful compilation.

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

After a good compilation, it will build directory in the current directory, and then go in, see a macosx-x86_64-normal-server-slowdebugis the ultimate directory.

IDEA is configured to use the compiled JDK

1. Open the IDEA, find File-> Project Structure.

2, add a JDK

3. Select the source above compiled jdk

4, when the project started last specify the JDK on it.

Debugging with CLion

1, open CLion, import the project, src directory location where the option to download the source code.

2, the configuration Debug Configurations, select Executable java executable is compiled, in the bin directory, and remove Build disposed.

Program arguments 设置为 -version,也可以设置其他的。设置为 -version 的意思是 java -version。

3、最后在源码中打个断点,比如 jni.cpp 或 thread.cpp 中,然后点击 debug ,就可以调试啦。

打造自己的 JDK

标题说的有点儿悬,打造自己的 JDK 哪儿有那么容易,况且还确实没那个实力。这里就是介绍一种思路,比如有些时候,我们调试 Java 代码最后发现走到了 JVM 层,这种情况下,我们就跟不进去了。执行到 JVM 层之后,里面的各种变量是怎么变换的我们就不知道了。这时候,我们找到 JVM 对应的代码稍微改一下,比如加个 printf 输出一下参数值就可以清晰的看出来了。

修改 JDK 代码

我在打开的 CLion 中找到了 java.c 文件的 JavaMain(void * _args) 方法,在里面加了一行打印代码,就勉强算实现了自己的 JDK 吧(微笑脸)。

万里长征第一步嘛,别的不重要,留下脚印儿才是关键。

printf("古时的风筝 JDK \n");

重新编译修改后的源码

修改之后,在终端中进入到源码目录的根目录,然后执行 make 命令。

因为之前已经编译过了,所以再次执行 make 是进行的增量编译,所以速度很快。

好了,见证奇迹的时刻到了

我们之前已经在 IDEA 中添加了编译好的 JDK,并且指定给了一个项目。仅为测试,代码如下。

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

当我们运行这个项目的时候,如果是平常的 JDK,会在控制台输出 hello jvm ,对不对。

可是,现在指定的不是平常的 JDK ,是被我加持过的 JDK 。

开始运行,输出的结果如下,看到没,刚刚加上的那行代码起作用了。

风筝说

真正能做到 JDK 定制开发的人并不多,我也完全没这个实力。但是每个 Java 开发者都编译一下 JDK 源码,翻一翻代码还是很有必要的。毕竟,我们每天写的代码都需要 JDK 的支持,都要跑在 JVM 上,我们就不好奇它们长成什么模样吗。

另外,这也可能为我们日常解决问题提供一种思路。有人说,最好的老师就是搜索引擎,大多数情况下是没错,但有的时候最好的方式往往就是看一眼源码。

为什么有的人解决问题的速度快,有些看似不能解决的问题放到大牛手里就能很快解决。有时候就是解决问题的维度不一样,人家是在三维的世界里,你却一直在二维的平面里转圈圈,比方说遇到程序问题,只能分析 Java 层面的问题这就是二维,进到 JDK、JVM 源码那就是进到的三维。维度高了,角度变了,解决问题的可能性和方式也就多了。这就好比三体里高等文明利用二向箔进行打击,完全不在一个体量下。

赶紧行动吧,编译一个你自己的 JDK。

给个推荐再走不迟

Guess you like

Origin www.cnblogs.com/fengzheng/p/12168903.html