r/javahelp 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!

7 Upvotes

24 comments sorted by

View all comments

3

u/morhp Professional Developer Feb 22 '22

Does the jar file contain the roll_bg.png in 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.

1

u/sparkless12 Feb 22 '22

well, since IDE run finds the correct file, I would presume yes, it does. JAR builder even has file printed out in console as packed without issues. Changing jar to zip, yes, relatively to class with resource call, image is ../ one step up in hierarchy and in /resources/ folder.

2

u/morhp Professional Developer Feb 22 '22

well, since IDE run finds the correct file, I would presume yes, it does.

The IDE typically doesn't run a jar file.

Changing jar to zip, yes, relatively to class with resource call, image is ../ one step up in hierarchy and in /resources/ folder.

Weird. If the file is there, Java should have found it. Please make sure that this.getClass() is the right class. It's usually safer to write something like MyClass.class.getResourceAsStream(...) as this could be something else, like a subclass or an anonymous inner class or whatever.

You might also want try something like MyClass.class.getResourceAsStream("/full/path/to/resources/roll_bg.png") (i.e. give it the complete package/path name with a slash at the start), as this rules out any problems with the relative location of your class.

JAR builder even has file printed out in console as packed without issues.

Not sure what this Jar Builder is you use, Google shows several unrelated projects with that name. Typically, people create jar files with Maven or Gradle.

1

u/sparkless12 Feb 22 '22 edited Feb 22 '22

Ill try to find class with static call then.

Im using no build tools.

Engine.class.getResourceAsStream("../resources/roll_bg.png") did not help

what do you mean by full path..? you mean define whole path from class path not relative one?