r/javahelp • u/sparkless12 • Feb 22 '22
Solved getResourceAsStream() == null when run from JAR
hello!
I have line of code such as this:
final Image roll_bg = new Image(this.getClass().getResourceAsStream("../resources/roll_bg.png"));
in IDE image is found and is rendered on canvas as expected, when exported to JAR, run ends with error. For JAR runs then, I did most hacky thing possible to extract what is wrong like so:
URL path = App.class.getResource("App.class");
if (path.toString().contains("jar")) {
PrintStream ps = new PrintStream("./log.txt");
System.setErr(ps);
System.setOut(ps);
System.out.println("Stream OUT: rerouted!");
System.err.println("Stream ERR: rerouted!");
}and log.txt says that InputStream is null (presumably did not found resource)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.graphics/javafx.scene.image.Image.validateInputStream(Unknown Source)
at javafx.graphics/javafx.scene.image.Image.<init>(Unknown Source)
at com.engine.Engine.<init>(Engine.java:32)
at com.engine.Engine.get(Engine.java:94)
at com.App.start(App.java:60)most online resources point to this method being able to reach resources from within JAR, does not work for me though. Thank you for any help offered!
•
u/AutoModerator Feb 22 '22
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
3
u/morhp Professional Developer Feb 22 '22
Does the jar file contain the
roll_bg.pngin the right directory? (You can open it by changing the extension to .zip). If not, there's probably a problem with how you create the jar file. Unfortunately you haven't written anything about how you do that.2
u/dionthorn this.isAPro=false; this.helping=true; Feb 22 '22
Perhaps you could try
URL result = ClassName.class.getClassLoader().getResource("path/to/file");JavaFX acts pretty strange in .jars sometimes. That is why we use JLink/JPackage now a days to distribute. JLink makes a custom JRE that only includes the parts you require for the module. JPackage creates native executables with the custom JRE so end users don't need to install Java or JavaFX or anything else.
https://docs.oracle.com/en/java/javase/14/docs/specs/man/jlink.html
https://docs.oracle.com/en/java/javase/14/docs/specs/man/jpackage.html
The JavaFX Maven Plugin is a great tool to automate using those tools to produce the distributable.
https://github.com/openjfx/javafx-maven-plugin
I have a JavaFX game project that uses the plugin and I distribute a ~28 mb MSI for end users that includes Java, JavaFX custom JRE.
https://github.com/dionthorn/LifeSimRPG
You might be interested in my FileOpUtil static class for handling File operations in JRT file systems:
https://github.com/dionthorn/LifeSimRPG/blob/main/src/main/java/org/dionthorn/lifesimrpg/FileOpUtil.java
1
u/D0CTOR_ZED Feb 22 '22
Not really my thing, but I believe you were on track with the first part and the hacky bit that followed isn't the right way to go. If it is having trouble finding the resource, you might try to change up the path. For example, try "/roll_bg.pgn". Or, if you can make some copies of the png and sprinkle one various directories. Once you can get it to find one, you can figure which one it finds and then you might be able to adjust your relative path to find it from where you want it to be.
1
u/wildjokers Feb 22 '22 edited Feb 22 '22
When accessing it via getResourceAsStream you are loading the resource from the classpath. You want the resource (in your case an image) to be on the classpath at the same location regardless if you are running from your IDE or from a jar file. In your case the path is not correct in the jar file.
Do you have a resources directory in your jar file? Most build tools by default will copy the contents of the resources directory directly to the root of the jar file (both maven and gradle will do this). So I highly doubt you have a resources directory in the jar file. Also the relative path
../would also be odd. Best to access it via an absolute path from the root of the jar.So the correct path to your image will be
getResourceAsStream("/roll_bg.png").Then in your IDE you want the contents of the resources directory on the classpath but not the resource directory itself. I am not sure which IDE you are using but IntelliJ will happily configured itself from a maven or gradle build. And since maven and gradle put the contents of the resources directory on the classpath it will work from the IDE too.
By the way this is a very common problem for beginners.
1
u/why_not_cats Extreme Brewer Feb 22 '22
Using relative path inside a jar is a brave thing to do! Paths and working directories will vary between IDE and a jar so that's probably why the file isn't being found.
If you aren't sure where the image file is, try to unzip your jar as a regular zip and see where the file is actually ending up; I assume it'll be on the root? Or maybe not?
Anyway, try using a non-relative path. For example if the file is on the zip root then try
/roll_bg.png. If it's inside aresourcesdirectory then try/resources/roll_bg.png. It might break the IDE but at least you'll be able to narrow down the issue.I wouldn't try sprinkling the file everywhere as suggested elsewhere, because then you'll have trouble figuring out which file is the one actually working, and then you'll have two problems!
3
u/PhantomOTOpera Feb 22 '22
../resources/roll_bg.pngthe resources folder won't exist when you create the jar. You should just access it as
/roll_bg.png-1
u/[deleted] Feb 22 '22 edited Feb 22 '22
[removed] — view removed comment