java.lang.IllegalAccessException:类z.y.x.u.SimpleCompileTest无法访问带有修饰符的类MyClass的成员&引用;
在使用反射API时,我遇到以下错误java.lang.IllegalAccessException:类z.y.x.u.SimpleCompileTest无法访问带有修饰符的类MyClass的成员&引用;,java,reflection,Java,Reflection,在使用反射API时,我遇到以下错误 java.lang.IllegalAccessException: Class z.y.x.u.SimpleCompileTest can not access a member of class MyClass with modifiers "" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65) at java.lang.Class.newInstance0(Cl
java.lang.IllegalAccessException: Class z.y.x.u.SimpleCompileTest can not access a member of class MyClass with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:351)
at java.lang.Class.newInstance(Class.java:310)
at z.y.x.u.SimpleCompileTest.reflectionCall(SimpleCompileTest.java:44)
at z.y.x.f.RunFunctionality.doPost(RunFunctionality.java:116)
at z.y.x.f.RunFunctionality.doGet(RunFunctionality.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
这是我的Psedu代码
public static void reflectionCall() {
ClassLoader classLoader = SimpleCompileTest.class.getClassLoader();
try{
Class aClass = classLoader.loadClass("MyClass");
Object t = aClass.newInstance();
System.out.println("aClass.getName() = " + aClass.getName());
Method method = aClass.getDeclaredMethod ("myMethod", null);
method.setAccessible(true);
method.invoke(t, null);
.....
}
它在发生以下错误时抛出错误
Object t = aClass.newInstance();
MyClass.class文件位于jar文件中,在执行MyClass.java的内容时,会在类路径中动态添加该文件
public class MyClass {
public MyClass() {
// TODO Auto-generated constructor stub
}
public void myMethod(){
System.out.println("My Method Called");
}
}
无法找出问题所在,请提供更多帮助。异常消息为
java.lang.IllegalAccessException: Class z.y.x.u.SimpleCompileTest can not access a member of class MyClass with modifiers “”
请注意“
中缺少修饰符值。类#newInstance()的javadoc声明
抛出:
IllegalAccessException-如果类或其空构造函数不可访问
换句话说,您的MyClass
构造函数是不可访问的。从你的代码来看似乎是这样,但我猜你是复制粘贴错了。它很可能缺少public
修饰符,即它具有默认的可访问性。由于该类和您的SimpleCompileTest
类位于不同的包中,因此构造函数不可见 异常消息为
java.lang.IllegalAccessException: Class z.y.x.u.SimpleCompileTest can not access a member of class MyClass with modifiers “”
请注意“
中缺少修饰符值。类#newInstance()的javadoc声明
抛出:
IllegalAccessException-如果类或其空构造函数不可访问
换句话说,您的MyClass
构造函数是不可访问的。从你的代码来看似乎是这样,但我猜你是复制粘贴错了。它很可能缺少public
修饰符,即它具有默认的可访问性。由于该类和您的SimpleCompileTest
类位于不同的包中,因此构造函数不可见 Spring Boot也有类似的问题,消息略有不同,但@sotirios delimanolis的解决方案有助于:
Class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor can not access a
member of class com.etc.bla.model.entity.BackendBaseEntity with modifiers "public"
BackendBaseEntity
类确实具有包可见性,并将其公开
解决了错误。在Spring Boot中也有类似的问题,但消息略有不同,但@sotirios delimanolis的解决方案有助于:
Class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor can not access a
member of class com.etc.bla.model.entity.BackendBaseEntity with modifiers "public"
BackendBaseEntity
类确实具有包可见性,将其设置为public
解决了错误。您确定该类就是这样的吗?看起来(从错误中)构造函数有默认的可见性,即没有访问修饰符。你确定这就是类吗?看起来(从错误中)构造函数具有默认可见性,即没有访问修饰符。