Java Reflection, java.lang.IllegalAccessException Error
- by rubby
Hi all,
My goal is :
Third class will read the name of the class as String from console.Upon reading the name of the class, it will automatically and dynamically (!) generate that class and call its writeout method. If that class is not read from input, it will not be initialized.
And I am taking java.lang.IllegalAccessException: Class deneme.class3 can not access a member of class java.lang.Object with modifiers "protected" error. And i don't know how i can fix it..
Can anyone help me?
import java.io.*;
import java.lang.reflect.*;
class class3
{
public void run()
{
try
{
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
String line=reader.readLine();
Class c1 = Class.forName("java.lang.String");
Object obj = new Object();
Class c2 = obj.getClass();
Method writeout = null;
for( Method mth : c2.getDeclaredMethods() ) {
writeout = mth;
break;
}
Object o = c2.newInstance();
writeout.invoke( o );
}
catch(Exception ee)
{
System.out.println("ERROR! "+ee);
}
}
public void writeout3()
{
System.out.println("class3");
}
}
class class4
{
public void writeout4()
{
System.out.println("class4");
}
}
class ornek {
public static void main(String[] args) {
System.out.println("Please Enter Name of Class : ");
class3 example = new class3();
example.run();
}
}