win10&Cygwin编译调试OpenJDK11(精简)
博主机器8G,尝试过虚拟机,编译成功了,调试实在时卡到爆,迫不得已使用本地编译
大家也可以尝试WSL2, 应该比虚拟机性能好些
windos 下编译jdk 坑较多,如果机器性能较好,建议使用Ubuntu
请严格按照下面教程来,特别是各个版本号,不同的版本目录结构不一样,比如visual studio,如果自己具备基本的shell编程能力,百度无法解决时,可以自行阅读相关shell文件
比如:build\.configure-support\generated-configure.sh
编译
-
下载openjdk11 源代码, 同时安装jdk10
OpenJDK 11 源代码:
https://github.com/openjdk/jdk,版本:jkd 11.27 切换tag下载, 解压
建议先阅读源码目录下doc/building.html编译文档, 形成一个良好的学习习惯
JDK10(需要登录): 作为编译引导jdk
https://download.oracle.com/otn/java/jdk/10.0.2+13/, 安装后可以不用设置环境变量
-
安装cygwin 及其依赖包
下载当前最新版本: 3.3.3
安装过程中会提示你设置安装目录以及选择镜像
安装相关依赖项:
切换 setup-x86_64.exe 文件所在目录, 输入命令:
./setup-x86_64 -q -P autoconf -P make -P unzip -P zip
-
下载安装visual studio 2017
https://my.visualstudio.com/Downloads?q=visual studio 2017&wt.mc_id=omsftvscom~older-downloads
下载需要登录微软账号
我下载的这个版本:
安装目录不要有空格,否则在configure命令过程中会出现找不到路径
启动后会提示你需要安装的内容,我这里选择了两个, 感觉
C++的桌面开发
可以省略
如果安装过程中目录忘记了更改,等待安装完成后,也可以将Visual studio里面的2017目录复制一份出来, 由于我之前并没有更改目录,因此导致了很多错误

完成后查看目录, 正常情况下面目录会有这些文件
-
配置环境
-
在开始菜单中找到cygwin64 terminal, 启动
-
切换到jdk11 源代码的目录
- cd /cygdriver/e/xxxx/openjdk11 (
/cygdriver
后面的路径为磁盘号/路径….)
- cd /cygdriver/e/xxxx/openjdk11 (
-
输入下面命令
# --with-boot-jdk: 参数表示jdk10 的路径, (\ )表示空格转义 # --with-tools-dir: 指定visual studio C的库文件位置, 这里使用转义路径会出问题 ./configure --with-target-bits=64 --enable-debug --with-jvm-variants=server --with-boot-jdk=/cygdrive/e/Program\ Files/java/jdk-10.0.2 --with-tools-dir=/cygdrive/e/2017/Community/VC/Auxiliary --disable-warnings-as-errors
成功的话会提示下面日志信息:
-
-
上面无错误,进行编译
输入:
make images
这里会报错,解决见最后
-
等待编译完成即可
完成后日志如下:
-
验证JDK
- 切换目录:CMD进入即可
build\windows-x86_64-normal-server-fastdebug\jdk\bin
-
输入java -version
有internal 字样就说明是自己编译生成的
调试
可以使用Clion,VS Code,甚至GDB,这里介绍使用Clion 2021.1调试
-
使用Clion 打开openjdk的目录
-
打开后会提示你使用CMAKE、MakeList工程,直接选择CMAKE 即可
-
导入源码后会自动对文件建立索引
-
设置C编译器、调试器, 一定得使用visual studio 的,不能使用自己下载的TDM,或则MingW,因为我们的jdk是用visual studio 中的库 编译的
可以看看这里是否正确
-
配置启动
-
断点开始Debug
打两个断点:
程序启动入口:
创建线程入口
启动调试
结果如下:
遇到的问题
configure执行问题
下面所列问题都是由于路径问题导致
Visual Studio installation not recognized
VS 路径的问题
查看配置文件,发现路径是vc/bin/vavars64.bat, 而我们的vavars64.bat 路径为vc/auxiliary/build/vavars64.bat, 修改即可
'E:/Program' is not recognized as an internal or external command
还是路径找不到,😥, 空格符的问题,这里可以使用转义解决, 使用 \ (一个反斜杠+空格) 代替空格
No such file or directory
依然是路径问题,上面已经转义为什么还是报错
在报错的地方路径写死
The C compiler (located as usage: cl [ option... ] filename... [ /link linkoption..) does not seem to be the required microsoft compiler
找不到microsoft compiler , 貌似是之前--with-tools-dir 参数(…/Community/VC/Auxiliary)少填了后面的Auxiliary导致
C compiler cannot create executables
checking whether the C compiler works... no
configure: error: in `/cygdrive/f/opensource/openjdk-jdk-11-27':
configure: error: C compiler cannot create executables
看下config.log 有没有特别的错误信息
路径问题,/cygdriver/e/program, 尝试解决一波
查看generated-configure.sh文件,找到相关内容输出地方
向上翻,找到可疑对象,ac_link_default
这里eval 表示的执行这个变量,在上面可以看到该变量的值为:
$CC $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS
eval意思就是执行这些变量,打印一个信息看看结果,得出下面内容
还是路径问题,不忍了,更改visual studio 路径,直接将program files (x86)/microsoft visual studio/2017 移动到根目录
执行新命令
./configure --with-target-bits=64 --enable-debug --with-jvm-variants=server --with-boot-jdk=/cygdrive/e/Program\ Files/java/jdk-10.0.2 --with-tools-dir=/cygdrive/e/2017/Community/VC/Auxiliary
成功了,浪费这么多时间😭
make执行问题
make images命令执行错误后,重新执行命令前,先执行make clean,在执行make images
_the.BUILD_TOOLS_LANGTOOLS_batch
building target 'images' in configuration 'windows-x86_64-normal-server-fastdebug'
make[3]: *** No rule to make target '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/buildtools/langtools_tools_classes/_the.BUILD_TOOLS_LANGTOOLS.vardeps', needed by '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/buildtools/langtools_tools_classes/_the.BUILD_TOOLS_LANGTOOLS_batch'. Stop.
make[3]: *** Waiting for unfinished jobs....
方案1: 由于默认按照的make为4.3,可以降低make版本为4.2
方案2:修改make/common/MakeBase.gmk, 参考https://blog.csdn.net/qq_50795908/article/details/118827283
如果配置更改错误,make images 可能会报下面错, 重新改下就可以了
make[3]: *** No rule to make target '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/buildtools/interim_langtools_modules/java.compiler.interim/_the.BUILD_java.compiler.interim.vardeps', needed by '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/buildtools/interim_langtools_modules/java.compiler.interim/_the.BUILD_java.compiler.interim_batch'. Stop.
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [make/Main.gmk:76: interim-langtools] Error 2
make[2]: *** Waiting for unfinished jobs....
make[3]: *** No rule to make target '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/hotspot/variant-server/libjvm/objs/jvm.comp.vardeps', needed by '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/hotspot/variant-server/libjvm/objs/BUILD_LIBJVM_pch.obj'. Stop.
hotspot/gtest/utilities/test_json.
直接删除test_json.cpp
* For target hotspot_variant-server_libjvm_gtest_objs_test_json.obj:
test_json.cpp
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2143: syntax error: missing ')' before ']'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(355): error C2660: 'JSON_GTest::test': function does not take 1 arguments
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(49): note: see declaration of 'JSON_GTest::test'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2143: syntax error: missing ';' before ']'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2059: syntax error: ']'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2017: illegal escape sequence
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2059: syntax error: ')'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(363): error C2143: syntax error: missing ')' before ']'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(361): error C2660: 'JSON_GTest::test': function does not take 1 arguments
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(49): note: see declaration of 'JSON_GTest::test'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(363): error C2143: syntax error: missing ';' before ']'
flags/jvmFlagRangeList
jvmFlagRangeList.cpp
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/runtime/flags/jvmFlagRangeList.cpp(341): error C2220: warning treated as error - no 'object' file generated
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/runtime/flags/jvmFlagRangeList.cpp(341): warning C4305: 'argument': truncation from 'const intx' to 'double'
看到这个字样:warning treated as error
重新执行configure: 加上--disable-warnings-as-errors参数即可解决
The file contains a character that cannot be represented in the current code page
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/compiler/methodMatcher.cpp(291): warning C4474: 'sscanf' : too many arguments passed for format string
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/compiler/methodMatcher.cpp(291): note: placeholders and their parameters expect 0 variadic arguments, but 2 were provided
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/runtime/objectMonitor.cpp(1): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/runtime/objectMonitor.cpp(1063): error C2065: 'Knob_QMode': undeclared identifier
这个问题是由于编码问题导致,我这里报错是因为我之前将objectMonitor 添加 了中文注释导致的,将源文件替换过来即可
参考资料:
https://www.freesion.com/article/755291723/
https://blog.csdn.net/wqlinloveruby/article/details/80409031
https://blog.csdn.net/qq_50795908/article/details/118827283
https://blog.csdn.net/quantum7/article/details/100084314
….