Reflection API - why am i not getting name of the class????? | Selenium Forum
M
Posted on 20/12/2015
class simple1{};
interface my1{};


class reflectionAPIExample1 {
public static void main(String[] args)
{

try {
Class c;
c = Class.forName("simple1");
System.out.println("" + c.getName()); /// CE ---class not found?????
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

I have created separte public class B & try to run below code, however same error class not found? can you guide me where i going wrong?
Class c=Class.forName("B");
Object o= c.newInstance();
Method m =c.getDeclaredMethod("message", null);
m.setAccessible(true);
m.invoke(o, null);
System.out.println("AAA");

in above code i am trying to create object of the class which is available in same package / different package. & trying to call method of that class. guide me....... i got stuck ............. :(

M
Replied on 21/12/2015

code you have provided is confusing can you provide the full code.


M
Replied on 21/12/2015

package collection;

class Simple{}
interface My{}

class Test{
public static void main(String args[]){
try{
Class c=Class.forName("Simple");
System.out.println(c.isInterface());

Class c2=Class.forName("My");
System.out.println(c2.isInterface());

}catch(Exception e){System.out.println(e);}

}
}


o/p -- java.lang.ClassNotFoundException: Simple


M
Replied on 22/12/2015

interface My {
}
class Simple {
}

public class Test {
public static void main(String args[]) {
try {
Class c = Simple.class;
System.out.println(c.isInterface());

Class c2 = My.class;
System.out.println(c2.isInterface());

} catch (Exception e) {
e.printStackTrace();
}

}
}