JavaAgent:查看版本 v5
由 mcst12345 于 2026-10-04 02:25 保存 · 1861 字节
Java自帶的調試接口,常用於運行時字節碼注入。
javaagent為一至少聲明有Premain-Class或Agent-Class屬性的jar,Premain-Class與Agent-Class為agent主類,Premain-Class類需定義public static void premain(String args, Instrumentation inst) 方法,Agent-Class類需定義public static void agentmain(String args, Instrumentation inst) 方法。
其他可選屬性為
* Can-Redefine-Classes 允許重定義類
* Can-Retransform-Classes 允許重轉換類
* Can-Set-Native-Method-Prefix 允許設置[[native method prefix]]
== 相关 ==
* 核心接口及获取方式见 [[Instrumentation]],JDK 内部实现见 [[InstrumentationImpl]],底层依赖 [[JVMTI]]。
* 目前最常用的加载方式是'''运行时外部进程附加''',不需要任何启动参数,详见 [[Instrumentation]] 的“运行时外部进程附加”一节。
Java's debug interface, commonly used for runtime class bytecode injection.
javaagent is a jar file with manifest attribute Premain-Class or Agent-Class, Premain-Class attribute and Agent-Class attribute declares the agent main class of a javaagent, the class speficied by Premain-Class should declare method public static void premain(String args, Instrumentation inst) , the class specified by Agent-Class should declare method public static void agentmain(String args, Instrumentation inst) .
Other optional attribute for a javaagent can be
* Can-Redefine-Classes
* Can-Retransform-Classes
* Can-Set-Native-Method-Prefix
== Related ==
* See [[Instrumentation]] for provided methods and how to load a javaagent, see [[InstrumentationImpl]] for default implementation at runtime, see [[JVMTI]] for underlying mechanism.
* "runtime attach" is the most commonly used method to load a javaagent at present, it doesn't require any commandline arguments, see [[Instrumentation]] for more details.