I am trying to load a Java file to my android project using URLClassLoader
class.
But I got a ClassNotFoundError
when I run the below code:
package com.xyz.abc;
public class ConstantClassReader
{
public void readFile() throws MalformedURLException
{
String url = "file://"+Environment.getExternalStorageDirectory().getPath()+"/StringConstants.class";
URLClassLoader urlClassLoader = URLClassLoader.newInstance(new URL[]{new URL(url)});
Class simpleClass = null;
try {
simpleClass = urlClassLoader.loadClass("com.xyz.abc.StringConstants");
Constructor simpleConstructor = simpleClass.getConstructor();
Object simpleClassObj = simpleConstructor.newInstance();
Method method = simpleClass.getMethod("myMethod");
method.invoke(simpleClassObj);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
The below Java file is resides inside my SDCard:
package com.xyz.abc;
public class StringConstants{
public void myMethod(){
System.out.println("myMethod Loaded");
}
}