我想调用默认访问类Demo的main方法:
class Demo {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
我从另一个类中调用它,例如:
String[] str = {};
Class cls = Class.forName(packClassName);
Method thisMethod = cls.getMethod("main", String[].class);
thisMethod.setAccessible(true);
thisMethod.invoke(cls.newInstance(), (Object) str);
但我得到了例外,即
java.lang.IllegalAccessException: Class javaedit.Editor can not access a member of class Demo with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
at java.lang.Class.newInstance0(Class.java:366)