JVM配置项-Dkotlinx.coroutines.debug

本文详细解析了如何通过配置-Dkotlinx.coroutines.debug来开启Kotlin协程的调试功能,包括常见错误与解决方法,以及如何正确设置JVM参数。

背景介绍

最近在学习Kotlin的Coroutine(协程)部分,在[ Coroutine Context and Dispatchers
](https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html
)这部分内容时,遇到下面这么一段话:
Run the following code with -Dkotlinx.coroutines.debug JVM option:
(即使用-Dkotlinx.coroutines.debug 这个JVM配置型来运行下面的代码)

-Dkotlinx.coroutines.debug

本来这么一行命令大可不必计较,但是一看官方栗子的运行结果,不禁虎躯一震。
运行结果
本来还在考虑协程这么light-weight怎么调试的问题,现在居然可以把协程编号打印出来。写好实例代码运行一看,此时的我是懵逼的,只有线程名称没有协程编号。
那就是:JVM配置项 -Dkotlinx.coroutines.debug 的问题啦。

配置-Dkotlinx.coroutines.debug

IDEA的JVM 配置项菜单在 Help | Edit Custom VM options下面,想进一步了解,请移步这里如下图所示:
idea.vmoptions

如果到这里就结束了,那这篇文章就没有必要啦!!! (Too young too simple…)
————————————————————————————————

真正的重点

1.上述方案的失败原因

Help -> Edit Custom VM Options 菜单是给Idea自己的执行设置JVM 配置项。

我们还需要给自己的应用设置JVM配置项
我们还需要给自己的应用设置JVM配置项
我们还需要给自己的应用设置JVM配置项

2. 给自己的项目设置配置项

Run | Edit Configurations 点击进入

画线部分是重点

即:-Dkotlinx.coroutines.debug=on

到这里就完了吗?你有可能还会出错…

如果太过随意在“=”的附近加上空格,请你还是CV操作吧…!!!
如果太过随意在“=”的附近加上空格,请你还是CV操作吧…!!!
如果太过随意在“=”的附近加上空格,请你还是CV操作吧…!!!

参考自:Stackoverflow

补充知识

kotlinx.coroutine.debug有三个属性值:
* auto 默认值:(在-ea才生效)
* on (等价于空字符串) :开启
* off :关闭

出处:Debug_property_name

D:\jdk-21.0.8\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:62183,suspend=y,server=n -javaagent:C:\Users\lyjj1\AppData\Local\JetBrains\IntelliJIdea2025.2\captureAgent\debugger-agent.jar=file:///C:/Users/lyjj1/AppData/Local/Temp/capture3146541087087115608.props -agentpath:C:\Users\lyjj1\AppData\Local\Temp\idea_libasyncProfiler_dll_temp_folder5588\libasyncProfiler.dll=version,jfr,event=wall,interval=10ms,cstack=no,file=C:\Users\lyjj1\IdeaSnapshots\DemoApplication_2025_12_03_225419.jfr,dbghelppath=C:\Users\lyjj1\AppData\Local\Temp\idea_dbghelp_dll_temp_folder1\dbghelp.dll,log=C:\Users\lyjj1\AppData\Local\Temp\DemoApplication_2025_12_03_225419.jfr.log.txt,logLevel=DEBUG -XX:TieredStopAtLevel=1 -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-Dmanagement.endpoints.jmx.exposure.include=*" -Dkotlinx.coroutines.debug.enable.creation.stack.trace=false -Ddebugger.agent.enable.coroutines=true -Dkotlinx.coroutines.debug.enable.flows.stack.trace=true -Dkotlinx.coroutines.debug.enable.mutable.state.flows.stack.trace=true -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath "E:\区块链\GZ036 区块链技术应用赛项赛题第1套附件 (1)\GZ036 区块链技术应用赛项赛题第1套附件\案例代码\FoodTraceability\backend\target\classes;E:\apache-maven-3.8.6\mvn-repo\org\springframework\boot\spring-boot-starter-web\2.6.2\spring-boot-starter-web-2.6.2.jar;E:\apache-maven-3.8.6\mvn-repo\org\springframework\boot\spring-boot-starter\2.6.2\spring-boot-starter-2.6.2.jar;E:\apache-maven-3.8.6\mvn-repo\org\springframework\boot\spring-boot\2.6.2\spring-boot-2.6.2.jar;E:\apache-maven-3.8.6\mvn-repo\org\springframework\boot\spring-boot-autoconfigure\2.6.2\spring-boot-autoconfigure-2.6.2.jar;E:\apache-maven-3.8.6\mvn-repo\org\springframework\boot\spring-boot-starter-logging\2.6.2\spring-boot-starter-logging-2.6.2.jar;E:\apache-maven-3.8.6\mvn-repo\ch\qos\logback\logback-classic\1.
最新发布
12-04
"C:\Program Files\Java\jdk1.8.0_202\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:49832,suspend=y,server=n -javaagent:C:\Users\wanghx1\AppData\Local\JetBrains\IdeaIC2025.2\captureAgent\debugger-agent.jar=file:///C:/Users/wanghx1/AppData/Local/Temp/capture3575731615809268923.props -Xms512M -Xmx512M -Xmn300M -Xss1M -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=15 -XX:PretenureSizeThreshold=1M -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Dloader.path=plugins -Dcom.sun.management.jmxremote.port=18999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dserver.port=8081 -agentpath:C:\Users\wanghx1\AppData\Local\Temp\idea_libasyncProfiler_dll_temp_folder\libasyncProfiler.dll=version,jfr,event=wall,interval=10ms,cstack=no,file=C:\Users\wanghx1\IdeaSnapshots\Debug_Assets_Plugin_2025_11_10_170737.jfr,dbghelppath=C:\Users\wanghx1\AppData\Local\Temp\idea_dbghelp_dll_temp_folder\dbghelp.dll,log=C:\Users\wanghx1\AppData\Local\Temp\Debug_Assets_Plugin_2025_11_10_170737.jfr.log.txt,logLevel=DEBUG -Dkotlinx.coroutines.debug.enable.creation.stack.trace=false -Ddebugger.agent.enable.coroutines=true -Dkotlinx.coroutines.debug.enable.flows.stack.trace=true -Dkotlinx.coroutines.debug.enable.mutable.state.flows.stack.trace=true -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2025.2.1\lib\idea_rt.jar" -jar D:\Gitlab\EAM\Debug\assets-infrastructure-service-1.0.0-RELEASE.jar --spring.profiles.active=dev 已连接到地址为 ''127.0.0.1:49832',传输: '套接字'' 的目标虚拟机 D:\Gitlab\EAM\Debug\assets-infrastructure-service-1.0.0-RELEASE.jar中没有主清单属性 已与地址为 ''127.0.0.1:49832',传输: '套接字'' 的目标虚拟机断开连接 进程已结束,退出代码为 1
11-11
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值