Why does getResourceAsStream() work in the IDE but not the JAR?
You can't use ..
with Class.getResourceAsStream()
.
To load a resource f.fsh
in the same package as the class, use SomeClass.class.getResourceAsStream("f.fsh")
To load a resource f.fsh
in a sub-package foo.bar of the package of the class, use SomeClass.class.getResourceAsStream("foo/bar/f.fsh")
To load a resource f.fsh
in any package com.company.foo.bar, use SomeClass.class.getResourceAsStream("/com/company/foo/bar/f.fsh")
This is described in the javadoc of the getResource()
method, although it lacks examples.
If ..
works in Class.getResourceAsStream()
while running from Eclipse, it's a bug in Eclipse. Eclipse and other IDEs implement custom class loaders to fetch resources from the project at runtime. It looks like the class loader implementation in Eclipse isn't performing all the necessary validations on input to getResourceAsStream()
method. In this case the bug is in your favor, but you will still need to rethink how you structure your resources for your code to work in all cases.