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

Cannot load resource #425

Closed
oemergenc opened this issue Aug 26, 2019 · 5 comments
Closed

Cannot load resource #425

oemergenc opened this issue Aug 26, 2019 · 5 comments
Labels

Comments

@oemergenc
Copy link

oemergenc commented Aug 26, 2019

Hey,

is there anything i am missing in loading a custom resource file (simple .properties file).
The project layout is like the following:

└── src
    ├── main
    │   ├── java
    │   │   └── de
    │   │       └── myproject
    │   │           └── mypackage
    │   │               └── HelloAction.java
    │   └── resources
    │       ├── META-INF
    │       │   └── plugin.xml
    │       └── my.properties

I am trying to load the file my.properties using the following code:

InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties");

However the resourceStream is always null. I also tried to copy the file into the libs folder and tried to add the file manually to the classpath. Unfortunately both did not work.
As far as a i know gradle, everything from the resource directory should be accessible on the classpath, shouldn't it?

The Project can be found here, it is based on the official getting started tutorial:
https://github.com/oemergenc/simple-intellij-plugin

@zolotov
Copy link
Member

zolotov commented Aug 26, 2019

gradle has nothing to do with the way how classes are loaded from jars. You can easily debug classloading.

in your particular case, you're trying to use classloader of Thread class for loading plugin-specific file. It's not allowed, you can access only to your plugin's classes. Just use your own class-loader: getClass().getResourcesAsStream("/my.properties"). Also, do not forget slash before.

@oemergenc
Copy link
Author

Hey, thanks for the clarification and your answer.

Unfortunately, this code comes from a third party library and was simplified by myself.
The code for loading the external resource properties file lays in a third party library, which i would like to include in my idea plugin. I just encapsulated the code to better demonstrate what my current problem is.

Is there any other way, you can think of, so that i can make this code working?

@zolotov
Copy link
Member

zolotov commented Aug 26, 2019

I don't think so. You can try to read your class/resources using plugin's classloader and then try to load your class/resource to root class-loader. Not sure it's possible, though.

@oemergenc
Copy link
Author

oemergenc commented Aug 27, 2019

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:


Thread.currentThread().setContextClassLoader(org.your.clazz.ProblemCode.class.getClassLoader());
InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.properties");

@adideas
Copy link

adideas commented Jan 4, 2023

I have a solution! I hope I'm not too late =)

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

3 participants