如何在类路径中放置javaagent的类 [英] How to put classes for javaagent in the classpath
问题描述
我正在尝试开发一个javaagent,它可以在asm-4的帮助下检测代码。现在我遇到了一个非常基本的问题,javaagent的类加载器没有看到asm依赖,因此失败了。我是否必须提供一个jar-with-dependencies(又名maven build plugin),其中包含代理程序所需的所有类,或者是否有另一种方法将类添加到java代理程序中?
直接在类路径中引用jar asm-all.jar没有帮助。
构建jar-with-dependencies最初没有帮助,因为无法使用程序集插件设置Premain-Class属性。
帮助表示赞赏; - )
I am trying to develop a javaagent that would instrument code with help of asm-4. For now I'm stucked with a pretty basic problem, the classloader for the javaagent doesn't see asm dependencies and therefor fails. Do I have to provide a jar-with-dependencies (aka maven build plugin) which contains all the needed classes by the agent, or is there another way to add classes to the java agent? Referencing the jar asm-all.jar directly in the classpath didn't help. Building jar-with-dependencies didn't help at first, because Premain-Class attribute couldn't be set with assembly plugin. Help is appreciated ;-)
推荐答案
好的,通过实验找到它。
依赖类应该是jar的一部分,可以通过maven程序集插件创建,例如:
ok, found it by experimenting. The dependent classes should be part of the jar, which can be created by maven assembly plugin, for example:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Premain-Class>test.agent.MyAgent</Premain-Class>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- this is used for inheritance merges -->
<phase>package</phase>
<!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
<!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
使用jar作为javaagent路径,一切正常。
Use the jar as javaagent path and everything works fine.
这篇关于如何在类路径中放置javaagent的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!