-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 open native image resource using Path, NativeImageResourceFileSystemProvider not initialised #7682
Comments
Note: adding:
...creates the filesystem. It only works for the native image run though, and fails when ran through an IDE or as a JAR as there's no provider for "resource". What's going on here, why isn't this registered as a provider to auto init / and picked up during an agent run? |
@jovanstevanovic: is this a bug? |
@credmond I'll have a look on Monday, and come back to you with an update. |
@credmond I couldn't reproduce it with the latest versions of both
|
Thanks for checking. But definitely is happening for me and I noticed it a couple of times (always just used workarounds). Let me see if I can get more details. What operating system, by the way? |
I forgot to mention, on Linux. |
I see. There are two problems:
From my perspective, this is not a bug. |
@jovanstevanovic: I mentioned above that manually opening the filesystem "works". However, I am very surprised to hear that that is by design and required. The same code ( I thought the point of native image combined with the tracing agent (and overall sensible native image config) was to translate your bytecode to a native image without requiring adding specific code to your application. Is this an exception for some reason? So I still thing this is a bug, or if not, it's an unclear design and is the only usage that requires adding hacky code, just for a native image to work. Registering a "resource:/" filesystem fails outside of a native image run. There is no such "resource" filesystem. This means we need to add hacky try/catch filesystem register code just to get the same code to be able to run in native image or via a JAR or IDE. I would have thought/expected that the filesystem could be marked for registering via native image config. The tracing agent surely should catch these odd requirements and add some configuration to register filesystems rather than requiring the developer to add them via application code, is that possible? I feel like I am missing something here. I can't see how requiring specific native image code, is expected or desired design. |
So you're telling me that if anyone wants to use basic standard Java code such as:
...with native image, then they have to also manually add a hack like this first in order to get the NativeImageResourceFileSystem registered:
... this is the reality. Is that the official expectation? I am not convinced this is a good idea. How native image looks up resources should be an internal problem and solved internally, Java application code should not need to consider this. I'm not sure if you're understanding my problem here. |
This is by design in Java as well. The Native Image FS operates similarly to how the
I'll describe you a couple of more details in the next comment. |
Here we have a couple of things.
|
The reason Additionally, when creating a file system in Java using a URL with a |
Thank you very much. I appreciate the details and yes, that makes sense. Oddly, the project where I noticed this originally must have some third party lib registering ZipFileSystemProvider somewhere (I cannot find it :/), because the same lookup "works" when running as a JAR without any intentional registering (but not as a native image), I see an old-ish enhancement request to auto-load filesystems: ...where it complains about "Path" being limited in this sense. I did not know this behaviour, and I'm surprised I haven't encountered it before in various projects. Anyway, although there's no bug as you said, hopefully this thread will help anyone with the same questions as myself. My advice:
Either way will work. Thanks again. |
@credmond You're welcome. I totally understand that these things with resources and file systems can be confusing sometimes. 🥲 |
Describe the issue
One cannot open a resource using Java's Path. It seems NativeImageResourceFileSystemProvider is not initialised (fileSystem is null hence the exception is thrown).
Steps to reproduce the issue
Try to open any resource using this (where fortunes.json exists in /resource/fortunes.json, for example):
Path storeResourcePath = Path.of(Fortune.class.getResource("/fortunes.json").toURI());
... you will get the stacktrace provided below.
Build steps:
E.g., using Graal's own examples @ https://github.com/graalvm/graalvm-demos/blob/master/fortune-demo/fortune/src/main/java/demo/Fortune.java, stick that line in main() and observe the same stacktrace pasted below.
It is possible to read the file this way:
InputStream stream = Fortune.class.getResource("/fortunes.json").openStream();
Obviously that's a workaround, but third party libraries using Path will still cause a problem.
I am surprised there isn't more noise about this issue.
Describe GraalVM and your environment:
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 17.0.9+11.1 (build 17.0.9+11-LTS-jvmci-23.0-b21, mixed mode, sharing)
Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.1+12.1 (build 21.0.1+12-jvmci-23.1-b19, mixed mode, sharing)
Stackrace:
The text was updated successfully, but these errors were encountered: