简体   繁体   English

Jar 文件作为 Java 中的代理

[英]Jar file as agent in java

When is jar file added as an agent in java and when it should be added in bootclasspath ?什么时候在java添加 jar 文件作为agent ,什么时候应该在bootclasspath添加?

Example : Why do we add lombok.jar as an agent when eclipse starts and why also we add in the bootclasspath ?示例:为什么我们在 eclipse 启动时添加lombok.jar作为agent ,为什么还要添加到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.首先,在 lombok 1.16.x 中不再需要将 lombok.jar 添加到引导类路径。

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.如果这些修改将来自您的程序的调用添加到仅在代理的 jar 中可用的代码,应用程序类加载器和验证器将针对程序的类路径解析这些调用,使用应用程序类加载器,而应用程序类加载器又使用父类加载器,一直向上到引导类加载器。 See the Oracle documentation for more information.有关更多信息,请参阅Oracle 文档

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.由于代理类加载器不在父类加载器结构中,因此您无法调用仅在代理的 jar 文件中可用的代码。

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.最简单的解决方案是将代理的 jar 添加到引导类路径,使代理 jar 中的代码可用于代理和程序。

In lombok 1.16.x, a different approach is used.在 lombok 1.16.x 中,使用了不同的方法。 The agent modifies the Eclipse classloader to also look in lombok.jar when resolving classes.代理会修改 Eclipse 类加载器,以便在解析类时也查看 lombok.jar。

Disclosure: I am a lombok developer.披露:我是龙目岛的开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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