M
Mani
Hi
I am loading a class dynamically and i am using classloader.My code
looks like this and its working fine.
import java.lang.reflect.Method;
public class TestTwo{
TestTwo(){}
void Create() throws Exception{
c.TestOne t ;
ClassLoader cl = this.getClass().getClassLoader();
Class cla = cl.loadClass("c.TestOne");
t = (c.TestOne) cla.newInstance();
System.out.println(t.getI());
Method[] methArr = cla.getDeclaredMethods();
for (int o=0; o< methArr.length; o++){
//To get Public methods out of the class
if(methArr[o].getModifiers()==java.lang.reflect.Modifier.PUBLIC)
System.out.println(methArr[o].getName());
}
}
public static void main(String[] args){
TestTwo t = new TestTwo();
try {
t.Create();
}
catch (Exception e) {e.printStackTrace();}
}
}
My doubt is i am having testone class inside this package
(c.testone)and suppose i am placing the class testone somewhere else
the code is not working fine..
My question is can we dynamically load a class dynamically from
anywhere in my system.
thanks for your reply in advance
I am loading a class dynamically and i am using classloader.My code
looks like this and its working fine.
import java.lang.reflect.Method;
public class TestTwo{
TestTwo(){}
void Create() throws Exception{
c.TestOne t ;
ClassLoader cl = this.getClass().getClassLoader();
Class cla = cl.loadClass("c.TestOne");
t = (c.TestOne) cla.newInstance();
System.out.println(t.getI());
Method[] methArr = cla.getDeclaredMethods();
for (int o=0; o< methArr.length; o++){
//To get Public methods out of the class
if(methArr[o].getModifiers()==java.lang.reflect.Modifier.PUBLIC)
System.out.println(methArr[o].getName());
}
}
public static void main(String[] args){
TestTwo t = new TestTwo();
try {
t.Create();
}
catch (Exception e) {e.printStackTrace();}
}
}
My doubt is i am having testone class inside this package
(c.testone)and suppose i am placing the class testone somewhere else
the code is not working fine..
My question is can we dynamically load a class dynamically from
anywhere in my system.
thanks for your reply in advance