Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load Plugin JAR Resource from Inside Plugin Code #548

Closed
Reyzerbit opened this issue Sep 20, 2023 · 3 comments
Closed

Load Plugin JAR Resource from Inside Plugin Code #548

Reyzerbit opened this issue Sep 20, 2023 · 3 comments
Labels

Comments

@Reyzerbit
Copy link

I understand that in order to load a resource from inside a plugin jar file, you need to use the Plugin Class Loader. I have two questions about that.

  1. Is it safe to pass the plugin class loader to the plugin itself via the PluginContext I created? My initial gut feeling is no, since you can access the parent/bootstrap classloader from the plugin class loader via getParent().
  2. If I am correct, and passing the plugin class loader to the plugin itself is not safe, how would I safely load a resource from the plugin jar file from inside the plugin code itself?
@decebals
Copy link
Member

decebals commented Sep 21, 2023

So, each available plugin is loaded using a different PluginClassLoader. Inside plugin, any getClass().getClassLoader() call will return the PluginClassLoader related to that plugin.
That being said, I don't see why you would pass the (Plugin)ClassLoader in PluginContext for the simple fact that it is redundant information.
The way to load a resource in a plugin remains the standard one from Java, getClass().getResource("/test.txt") or getClass().getClassLoader().getResourceAsStream("test.txt").
Yes, in some situations you can use getClass().getClassLoader().getParent() to access the parent class loader.
Try to play a little bit with a POC and share with us your project (GitHub) if you have questions or you encounter problems.
I hope what I said is clear, if not, please refine your questions.

@decebals
Copy link
Member

decebals commented Oct 3, 2023

@Reyzerbit Do you still need help? Can we close this issue?

@Reyzerbit
Copy link
Author

@decebals yes I managed to fix it using the information you provided. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants