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

Loading default Resource points to night-config's JAR, not to resources folder #128

Closed
brainbytes42 opened this issue Jan 3, 2023 · 3 comments
Labels
core ⚙️ doc The documentation / wiki should be improved

Comments

@brainbytes42
Copy link

Hi,

thanks for this library!

I wanted to specify a default config file in my resources-folder using FileConfig.builder(configPath).defaultResource(default), but the resource won't be found, as the FileNotFoundAction points to the night-config's JAR:

return copyData(FileNotFoundAction.class.getResource(resourcePath));

FileNotFoundAction.class.getResource("") = jar:file:/C:/Users/[...]/.gradle/caches/[...]/com.electronwill.night-config/core/3.6.6/[...]/core-3.6.6.jar!/com/electronwill/nightconfig/core/file/

I'd suggest return copyData(Thread.currentThread().getContextClassLoader().getResource(resourcePath)); to replace this line. This resolves correctly to the resources directory (.../build/resources/main/).

@mani1232
Copy link

mani1232 commented Jan 9, 2023

@TheElectronWill
Copy link
Owner

TheElectronWill commented Jan 14, 2023

Thanks for reporting!
You could use FileConfigBuilder#defaultData, or onFileNotFound with copyData + getResource on your class or classloader, so that is resolves properly.

edit: after some testing, it seems that it works fine when the resource path is absolute (e.g. /resource.json). Relative paths, however, do not work, but IMO they've always been a bit fragile to work with 😄

@TheElectronWill
Copy link
Owner

A note about the getContextClassLoader: I'm reluctant to use it, because it's more like an artifact of the past than a proper way to get "the right" classloader. Also, it may not exist in some environments.

The best solution is to pass an absolute path to your resource, or to call YourClass.class.getResource yourself. See Class#getResource.

The docs and wiki should be updated to reflect this.

@TheElectronWill TheElectronWill added core ⚙️ doc The documentation / wiki should be improved labels Jan 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core ⚙️ doc The documentation / wiki should be improved
Projects
None yet
Development

No branches or pull requests

3 participants