How to Load class object from source project ? How to use 'UrlClassLoader'?
已回答
Hi,
I'm trying to invoke an API and this API invokes ‘Class.forname()’,then I got a 'ClassNotFoundException' .
Here is a similar post,but it doesn't work for me:
My code:
List<URL> urls = new ArrayList<>();
pluginModule.getAllIdeaModules().values().forEach(
module -> {
try {
urls.add(VfsUtilCore.virtualToIoFile(CompilerModuleExtension.getInstance(module).getCompilerOutputPath()).toURI().toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
);
ClassLoader original = Thread.currentThread().getContextClassLoader();
UrlClassLoader cl=UrlClassLoader.build().parent(original).useCache().urls(urls).allowBootstrapResources().get();
ClassLoaderUtil.runWithClassLoader(cl,()-> invokeApi()); //get ClassNotFoundException
ClassLoaderUtil.runWithClassLoader(cl,()->Class.forName("myProject.class.fqcn"));//get ClassNotFoundException
Is there anything wrong with my code?Or are there other ways to achieve to load class object from source code into plugin?
请先登录再写评论。
What do you want to achieve with your plugin? Why do you set custom classloader parent's classloader to plugin's classloader?
Note contents of compiler output directories may be missing, incomplete or changing exactly at the moment when this code is executed.