How to load classes in a plugin with ServiceLoader? Follow
I'm the developer of PMDPlugin. In the latest version of PMD, it loads certain classes using ServiceLoader and it is not working inside the plugin. It is structured as follows:
I have the source of plugin and added the external library jars. The ServiceLoader definitions (META-INF/services) are inside the jars and it fails to load. I think the issue is because the libraries are loaded with a different classloader. I tried to add the path of the jars to that classloader, but still it fails. Any help to fix this is appreciated.
Please sign in to leave a comment.
What is the exact structure of your plugin, and do you see this problem when running from IntelliJ IDEA or in a packaged plugin? The library jars in the "lib" directory of a plugin are loaded with the same classloader as the classes of the plugin itself.
The services folder is in pmd-java-5.2.0.jar which is what the classloader is trying to load. I'm running it from Intellij. Haven't tested by creating a package.
Just noticed that if I do Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader() it is able to load. Not sure if this could have other side effects.
Just chiming in to say the above fix worked for me. Not sure why this is necessary, but thanks!
Hopefully, this issue describe why ServiceLoader doesn't work in plugin https://youtrack.jetbrains.com/issue/IDEA-241229