gpt4 book ai didi

java - AspectJ:LTW 中的 ExceptionInInitializerError

转载 作者:行者123 更新时间:2023-12-02 04:00:45 25 4
gpt4 key购买 nike

我尝试完成 AspectJ Cookbook 中的 LTW 示例,但它不起作用。我创建并编译了 Java 类:

public class MyClass{
public void foo(int number, String name){
System.out.println("Inside of foo");
}

public static void main(String[] args) {
MyClass myObject = new MyClass();
myObject.foo(1, "Str");
}
}

它工作得很好:

c:\TEMP\examples>java MyClass
Inside of foo

然后我创建了方面:

public aspect HelloWorld{
pointcut callPointcut(): call(void MyClass.foo(int, String));

before() : callPointcut(){
System.out.println("Hello World from advice");
}
}

编译它:

c:\TEMP\examples>c:\tools\aspectj1.8\bin\ajc -outjar my.jar HelloWorld.aj
C:\TEMP\examples\HelloWorld.aj:4 [warning] no match for this type name: MyClass [Xlint:invalidAbsoluteTypeName]
pointcut callPointcut(): call(void MyClass.foo(int, String));
^^^^^^^^^
[Xlint:invalidAbsoluteTypeName]

1 warning

然后尝试运行:

c:\TEMP\examples>java -classpath "C:\tools\aspectj1.8\lib\aspectjweaver.jar" "-Djava.system.class.loader=org.aspectj.weaver.loadtime.WeavingURLClassLoader" -Daj.class.path=. "-Daj.aspect.path=c:\TEMP\examples" MyClass
java.lang.ExceptionInInitializerError
at org.aspectj.weaver.WeaverMessages.<clinit>(WeaverMessages.java:18)
at org.aspectj.weaver.bcel.ClassPathManager.addPath(ClassPathManager.java:81)
at org.aspectj.weaver.bcel.ClassPathManager.<init>(ClassPathManager.java:63)
at org.aspectj.weaver.bcel.BcelWorld.<init>(BcelWorld.java:285)
at org.aspectj.weaver.tools.WeavingAdaptor.init(WeavingAdaptor.java:176)
at org.aspectj.weaver.tools.WeavingAdaptor.<init>(WeavingAdaptor.java:109)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:75)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:52)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
Caused by: java.lang.IllegalStateException: recursive invocation
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
at java.util.ServiceLoader.loadInstalled(Unknown Source)
at java.util.ResourceBundle.<clinit>(Unknown Source)
... 17 more
Error occurred during initialization of VM
java.lang.ExceptionInInitializerError
at org.aspectj.weaver.WeaverMessages.<clinit>(WeaverMessages.java:18)
at org.aspectj.weaver.bcel.ClassPathManager.addPath(ClassPathManager.java:81)
at org.aspectj.weaver.bcel.ClassPathManager.<init>(ClassPathManager.java:63)
at org.aspectj.weaver.bcel.BcelWorld.<init>(BcelWorld.java:285)
at org.aspectj.weaver.tools.WeavingAdaptor.init(WeavingAdaptor.java:176)
at org.aspectj.weaver.tools.WeavingAdaptor.<init>(WeavingAdaptor.java:109)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:75)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:52)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
Caused by: java.lang.IllegalStateException: recursive invocation
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)
at java.util.ServiceLoader.loadInstalled(Unknown Source)
at java.util.ResourceBundle.<clinit>(Unknown Source)
at org.aspectj.weaver.WeaverMessages.<clinit>(WeaverMessages.java:18)
at org.aspectj.weaver.bcel.ClassPathManager.addPath(ClassPathManager.java:81)
at org.aspectj.weaver.bcel.ClassPathManager.<init>(ClassPathManager.java:63)
at org.aspectj.weaver.bcel.BcelWorld.<init>(BcelWorld.java:285)
at org.aspectj.weaver.tools.WeavingAdaptor.init(WeavingAdaptor.java:176)
at org.aspectj.weaver.tools.WeavingAdaptor.<init>(WeavingAdaptor.java:109)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:75)
at org.aspectj.weaver.loadtime.WeavingURLClassLoader.<init>(WeavingURLClassLoader.java:52)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.lang.SystemClassLoaderAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.lang.ClassLoader.initSystemClassLoader(Unknown Source)
at java.lang.ClassLoader.getSystemClassLoader(Unknown Source)

我尝试不同的解决方案,仅当我提供 -Daj.aspect.path=c:\TEMP\examples 时才会出现异常,但据我了解,这是因为编织器将仅加载提供此选项的方面。如果没有此选项,Java 程序将无需在加载时编织任何方面即可运行。

最佳答案

你能使用javaagent吗?我感觉比修改类加载器更简单:

javac MyClass.java

ajc HelloWorld.java -1.8 -outxml -d out

export CLASSPATH=.:out:$CLASSPATH

java -javaagent:$AJHOME/lib/aspectjweaver.jar MyClass
Hello World from advice
Inside of foo

ajc 调用中的 -outxml 选项会在 out 文件夹中生成 META-INF/aop-ajc.xml 文件,这就是代理将寻找打开编织的内容。 (该 xml 文件将简单地列出要“打开”的方面以及可选的一些额外的编织器配置)

关于java - AspectJ:LTW 中的 ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34942134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com