学习笔记——解决一切Java问题的能力

Java 世界里的一切东西都只是在拼 Java 命令行参数而已

比如,你运行的 IDEA 中一段简单的代码,可以复制 console 的参数出来运行,发现结果一样:

"C:/Program Files/Java/jdk1.8.0_121/bin/java.exe" "-javaagent:C:/Program Files/JetBrains/IntelliJ IDEA 2019.2.4/lib/idea_rt.jar=3086:C:/Program Files/JetBrains/IntelliJ IDEA 2019.2.4/bin" -Dfile.encoding=UTF-8 -classpath "C:/Program Files/Java/jdk1.8.0_121/jre/lib/charsets.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/deploy.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/access-bridge-64.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/cldrdata.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/dnsns.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/jaccess.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/jfxrt.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/localedata.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/nashorn.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/sunec.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/sunjce_provider.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/sunmscapi.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/sunpkcs11.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/ext/zipfs.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/javaws.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/jce.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/jfr.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/jfxswt.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/jsse.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/management-agent.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/plugin.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/resources.jar;C:/Program Files/Java/jdk1.8.0_121/jre/lib/rt.jar;D:/OneDrive/02-coding/02-Java/demo/RabbitMQ/out/production/RabbitMQ" Dog

再比如,看看 mvn
这个命令到底执行了什么?

# 查看 mvn 的路径
which mvn
# 获得 mvn 路径,比如 /c/Program Files/apache-maven-3.5.2/bin/mvn,可以看到执行的具体命令
bash -x /c/Program Files/apache-maven-3.5.2/bin/mvn

可以看到,最后输出了这样一段:

exec /c/ProgramData/Oracle/Java/javapath/java -classpath '/c/Program Files/apache-maven-3.5.2-bin/apache-maven-3.5.2/boot/plexus-classworlds-2.5.2.jar' '-Dclassworlds.conf=/c/Program Files/apache-maven-3.5.2-bin/apache-maven-3.5.2/bin/m2.conf' '-Dmaven.home=/c/Program Files/apache-maven-3.5.2-bin/apache-maven-3.5.2' '-Dlibrary.jansi.path=/c/Program Files/apache-maven-3.5.2-bin/apache-maven-3.5.2/lib/jansi-native' '-Dmaven.multiModuleProjectDirectory=/c/Program Files/apache-maven-3.5.2-bin/apache-maven-3.5.2/bin' org.codehaus.plexus.classworlds.launcher.Launcher

调试一切 Java 程序

  • 想清楚要调试的代码跑在那个 JVM
  • 找到要调试的源代码

搜索关键词 java debug parameters
获得debug的命令行参数 -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044

运行如下调试命令:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044 Main
# 输出如下内容
Listening for transport dt_socket at address: 1044

等待 1044 的调试器连接过来,默认是 5005 端口

suspend

一旦启动如下环境变量之后,启动任何java程序,都会加上这个调试的参数:

# 设置环境变量
export JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=1044
# 运行需要调试的命令
mvn compile
# unset JAVA_TOOL_OPTIONS`

  • 在需要调试的代码的地方加上断点
  • 然后在 IDEA 的 configuration 中添加一个 remote 配置,连接到 1044 端口
  • 接着就可以在 IDEA 中运行 debug 调试了

原文 

https://michael728.github.io/2019/11/11/java-basic-skills-debug/

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 学习笔记——解决一切Java问题的能力

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址