ClassLoader loader = new ClassLoader();
loader = AccessController.doPrivileged(
new PrivilegedAction<T>(){
public T run(){
return new T();
}
}
)
// Usually the T is URLClassLoader or other user defined ClassLoader which implemented URLClassLoader
// return new URLClassLoader(new URL[]{});
Thread.getCurrentThread.setContextClassLoader(loader);
Class initialClass = loader.loadClass(“com.company.main.Entrance”);
Object instance = initialClass.newInstance();
Method startup = initialClass.getMethod(“start”, new Class[]{new String[0].getClass()});
Startup.invoke(*********);
User Defined ClassLoader Class By JMeter:
/**
* This is a basic URL classloader for loading new resources
* dynamically.
*
* It allows public access to the addURL() method.
*
* It also adds a convenience method to update the current thread classloader
*
*/
publicclass DynamicClassLoader extends URLClassLoader {
public DynamicClassLoader(URL[] urls) {
super(urls);
}
public DynamicClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
public DynamicClassLoader(URL[] urls, ClassLoader parent,
URLStreamHandlerFactory factory) {
super(urls, parent, factory);
}
// Make the addURL method visible
@Override
publicvoid addURL(URL url) {
super.addURL(url);
}
/**
*
* @param urls - list of URLs to add to the thread's classloader
*/
publicstaticvoid updateLoader(URL [] urls) {
DynamicClassLoader loader
= (DynamicClassLoader) Thread.currentThread().getContextClassLoader();
for(URL url : urls) {
loader.addURL(url);
}
}
}