Load-time weaving and java -jar

后端 未结 1 1954
臣服心动
臣服心动 2021-01-15 14:18

Is it possible to add the aspectj load-time agent when start a program with -jar ?

With Jetty, if I start

java -javaagent:aspectjweaver-1.8.0.jar -c         


        
相关标签:
1条回答
  • 2021-01-15 14:56

    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
    

    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.

    0 讨论(0)
提交回复
热议问题