Access Resources bundled within Plugin
Hello!
I want to access some images (as Files, not Icons) and one json file which I bundle with my plugin.
I have the structure
src/
main/
java/
res/
In the debug builds the acces works fine via:
final PluginId pluginId = PluginId.getId("<id>");
final IdeaPluginDescriptor pluginDescriptor = PluginManager.getPlugin(pluginId);
if (pluginDescriptor == null) {
return null;
}
final ClassLoader classLoader = pluginDescriptor.getPluginClassLoader();
final URL resource = classLoader.getResource(filePath);
But after I create a deploy build and install it, that is not working anymore. What am I doing wrong?
I even compared the structure of the generated plugin zips. In the debug build the content of my plugin is extracted to a "classes" folder, on the other hand in the deployment build, it is still a jar.
Thanks a lot!
Marc
Please sign in to leave a comment.
Yeah. I too got bitten by it. The debug environment unpacks the jar while the deployed environment does not. So the resources are not available in deployed version same as in debug. I would prefer that the debug version mimicked the deployed one, otherwise it makes it harder to debug.
Here is what I use to access a font resource. I create a file in the temp directory by copying the resource stream. For everything else that can be represented as a string it is easier to just grab the text of the resource with Resources.toString(resourceFile, Charsets.UTF_8)
Can't vouch that there is no better way. I am not a Java Guru. It does solve the problem for binary files.
For anyone experiencing the same problem. I solved it by using the using the Classloader of the thirdparty class containing the code above and setting it on the current thread. Something like the following:
[I have a solution!](https://gist.github.com/adideas/0aefc526d22b5a5f3833361dab9acbff) I hope I'm not too late =)