ClassNotFoundException during Dynamic Class loadin

2019-08-09 04:23发布

问题:

I am creating an app, where i am supposed to receive data through web service.

Here i am having a class, stored in the main package. I need to load data of this class dynamically i.e at run time, whenever something is received from the web service. But i am getting excepion of classNotFound.

Here is my code for dynamic loading of class,

try
{
    Class myClass = ClassLoader.getSystemClassLoader().loadClass("com.myPackage.it.ClassToLoad");
    Log.d("Dynamic class Loading", "1");
    Object DPMInstance = myClass.newInstance();
    Toast.makeText(getApplicationContext(), 
          "Get Methods" + myClass.getMethods(), 5000).show();
    Method myMethod = myClass.getMethod("sayHello", new Class[] {});      
    myMethod.invoke(DPMInstance, new Object[] { }); 
}
catch(Exception e)
{
    Log.d("Error", "" + e);
}

I am getting excpetion at this line,

Class myClass = ClassLoader.getSystemClassLoader().loadClass("com.myPackage.it.ClassToLoad");