是否可以在没有命令行参数的情况下在 JVM 启动后(又名:在运行时)启动 java jdwp?

Is it possible to start java jdwp after JVM startup (aka: at runtime) without command line parameters?

我想在不添加命令行参数的情况下在当前 JVM 的某个时刻启用调试 -agentlib:jdwp

是否可以在当前 运行 JVM 中以编程方式执行此操作? 没有任何第三方库 ?

可以考虑其他命令行参数(如-Djdk.attach.allowAttachSelf=true

VirtualMachine vm = VirtualMachine.attach(Long.toString(ProcessHandle.current().pid()));
vm.loadAgentLibrary("jdwp", "transport=dt_socket,server=y,address=8000,suspend=n");

原因:

com.sun.tools.attach.AgentLoadException: Failed to load agent library: _Agent_OnAttach@12 is not available in jdwp

在现代 JVM (Java 6+) 中,代理使用 JVM TI 接口。

The JVM Tool Interface (JVM TI) is a programming interface used by development and monitoring tools. It provides both a way to inspect the state and to control the execution of applications running in the Java virtual machine (VM).

在 JVM TI 内部,您必须启用所需的功能

The capabilities functions allow you to change the functionality available to JVM TI--that is, which JVM TI functions can be called, what events can be generated, and what functionality these events and functions can provide.

何时(JVM 的哪个状态)可以添加哪些功能取决于供应商。 JDWP只是JVM和调试器之间调试的协议。就像任何其他代理一样,它只是利用 JVM TI 的功能。同时,最有可能的是,调试功能只能在 OnLoad 阶段添加(在大多数 JVM 中)。 (即:can_generate_breakpoint_eventscan_suspend、...)

Add Capabilities: Typically this function is used in the OnLoad function. Some virtual machines may allow a limited set of capabilities to be added in the live phase.

这解释了为什么必须在 JVM 启动时声明 jdwp 代理以便向 JVM TI 添加适当的功能。

文档:https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#capability

感谢@Holger 指出了方向。