简体   繁体   中英

Jar file as agent in java

When is jar file added as an agent in java and when it should be added in bootclasspath ?

Example : Why do we add lombok.jar as an agent when eclipse starts and why also we add in the bootclasspath ?

 -javaagent:/home/$USER/eclipse/4.4-luna-SR2/lombok.jar
 -Xbootclasspath/a:/home/$USER/eclipse/4.4-luna-SR2/lombok.jar

First of all, in lombok 1.16.x it is no longer necessary to add lombok.jar to the bootclasspath.

Agents live in their own classpath, but share the bootclasspath with the regular program.

Agents typically use instrumentation to modify the bytecode of the classes in your program. If those modifications add calls from your program to code that's only available in the agent's jar, the application classloader and the verifier will resolve those calls against the program's classpath, using the application classloader that in its turn uses the parent classloader, all the way up to the bootclassloader. See the Oracle documentation for more information.

Since the agent classloader is not in the parent classloader structure, you cannot call code that's only available in the agent's jar file.

The simplest solution is to add the agent's jar to the bootclasspath, making the code in the agent's jar available for both the agent and the program.

In lombok 1.16.x, a different approach is used. The agent modifies the Eclipse classloader to also look in lombok.jar when resolving classes.

Disclosure: I am a lombok developer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM