加载时编织和 java -jar [英] Load-time weaving and java -jar
问题描述
是否可以在使用 -jar 启动程序时添加 aspectj 加载时代理?
Is it possible to add the aspectj load-time agent when start a program with -jar ?
使用 Jetty,如果我开始
With Jetty, if I start
java -javaagent:aspectjweaver-1.8.0.jar -classpath toto.jar -jar start.jar
aspectj 不适用.但我如果打电话
The aspectj is not apply. But I if call
java -javaagent:aspectjweaver-1.8.0.jar -classpath toto.jar;start.jar org.eclipse.jetty.start.Main
一切都很好.
推荐答案
-jar
和 LTW 的问题似乎是类加载顺序与 -cp (...) my.MainClass
方法.可能在织入者可以在类路径上的方面 JAR 中看到方面和 META-INF/aop-ajc.xml
之前加载了用 -jar
指定的 JAR.我找到了一个解决方法,但它并不好:您可以将切面 JAR 放在 JVM 的引导类路径上,但在这种情况下,切面甚至在编织代理之前加载.因此,您还必须将 aspectjrt.jar
添加到引导类路径:
The problem with -jar
and LTW seems to be that there is something different with the classloading order compared to the -cp (...) my.MainClass
approach. Probably the JAR specified with -jar
is loaded before the weaver can see the aspects and META-INF/aop-ajc.xml
in the aspect JAR on the classpath. I found a workaround, but it is not nice: You can put the aspect JAR on the JVM's boot classpath, but in this case the aspects are loaded even before the weaving agent. Thus you also have to add aspectjrt.jar
to the boot classpath:
java -Xbootclasspath/a:aspect.jar;lib\aspectjrt.jar -javaagent:lib\aspectjweaver.jar -jar application.jar
考虑到这种情况,我想最好将应用程序 JAR 添加到普通类路径并手动指定主类.但这是一个品味问题,以及您在启动应用程序时是否知道主类名称.
Considering this circumstance, I guess it is preferable to add the application JAR to the normal classpath and specify the main class manually. But it is a matter of taste and whether you know the main class name when starting your application.
这篇关于加载时编织和 java -jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!