Having trouble finding your resources when your exported your files to a JAR?
Step 1: never use [icode]File[/icode] or [icode]FileInputStream[/icode] to lookup resources in a JAR
Didn’t that quite solve your problem?
Step 2: put this code in your application:
- http://pastebin.java-gaming.org/5a11a234c9e (v1.0, 2014-05-18)
- http://pastebin.java-gaming.org/5833c5c324011 (v1.1, 2016-04-07)
Now it’s time to figure out why your resource cannot be found:
Step 3: insert this line where error occurs
ClassLoaderDummy.searchResource("res/images/sprite.jpg");
Example input:
public static void main(String[] args) throws IOException {
ClassLoaderDummy.searchResource("sprite.jpg");
ClassLoaderDummy.searchResource("images/res/Sprite.JPG");
ClassLoaderDummy.searchResource("images/sprite.jpg");
ClassLoaderDummy.searchResource("res/images/sprite.jpg");
ClassLoaderDummy.searchResource("res/images/Sprite.jpg");
ClassLoaderDummy.searchResource("sprite.png");
ClassLoaderDummy.searchResource("huh.png");
}
Example output:
[ClassLoaderDummy] SEARCHING: "sprite.jpg"
[ClassLoaderDummy] FOUND: mislocated, similarly named resource:
[ClassLoaderDummy] "res/images/Sprite.JPG"
[ClassLoaderDummy] in classpath entry:
[ClassLoaderDummy] "D:\workspace\workspace4\Misc\bin"
[ClassLoaderDummy] for access use:
[ClassLoaderDummy] getResourceAsStream("/res/images/Sprite.JPG");
[ClassLoaderDummy] SEARCHING: "images/res/Sprite.JPG"
[ClassLoaderDummy] FOUND: mislocated resource:
[ClassLoaderDummy] "res/images/Sprite.JPG"
[ClassLoaderDummy] in classpath entry:
[ClassLoaderDummy] "D:\workspace\workspace4\Misc\bin"
[ClassLoaderDummy] for access use:
[ClassLoaderDummy] getResourceAsStream("/res/images/Sprite.JPG");
[ClassLoaderDummy] SEARCHING: "images/sprite.jpg"
[ClassLoaderDummy] FOUND: mislocated, similarly named resource:
[ClassLoaderDummy] "res/images/Sprite.JPG"
[ClassLoaderDummy] in classpath entry:
[ClassLoaderDummy] "D:\workspace\workspace4\Misc\bin"
[ClassLoaderDummy] for access use:
[ClassLoaderDummy] getResourceAsStream("/res/images/Sprite.JPG");
[ClassLoaderDummy] SEARCHING: "res/images/sprite.jpg"
[ClassLoaderDummy] FOUND: similarly named resource:
[ClassLoaderDummy] "res/images/Sprite.JPG"
[ClassLoaderDummy] in classpath entry:
[ClassLoaderDummy] "D:\workspace\workspace4\Misc\bin"
[ClassLoaderDummy] for access use:
[ClassLoaderDummy] getResourceAsStream("/res/images/Sprite.JPG");
[ClassLoaderDummy] SEARCHING: "res/images/Sprite.jpg"
[ClassLoaderDummy] FOUND: similarly named resource:
[ClassLoaderDummy] "res/images/Sprite.JPG"
[ClassLoaderDummy] in classpath entry:
[ClassLoaderDummy] "D:\workspace\workspace4\Misc\bin"
[ClassLoaderDummy] for access use:
[ClassLoaderDummy] getResourceAsStream("/res/images/Sprite.JPG");
[ClassLoaderDummy] SEARCHING: "sprite.png"
[ClassLoaderDummy] FOUND: similarly named resource with different extension:
[ClassLoaderDummy] "res/images/Sprite.JPG"
[ClassLoaderDummy] in classpath entry:
[ClassLoaderDummy] "D:\workspace\workspace4\Misc\bin"
[ClassLoaderDummy] for access use:
[ClassLoaderDummy] getResourceAsStream("/res/images/Sprite.JPG");
[ClassLoaderDummy] SEARCHING: "huh.png"
[ClassLoaderDummy] FAILED: failed to find anything like
[ClassLoaderDummy] "huh.png"
[ClassLoaderDummy] in all classpath entries:
[ClassLoaderDummy] "D:\workspace\workspace4\Misc\bin"
[ClassLoaderDummy] "D:\workspace\workspace4\LibBase\bin"
[ClassLoaderDummy] "D:\workspace\workspace4\LibComponents\bin"
[ClassLoaderDummy] "D:\workspace\workspace4\LibMedia\bin"
[ClassLoaderDummy] "D:\workspace\workspace4\LibMedia\lwjgl_util.jar"
[ClassLoaderDummy] "D:\workspace\workspace4\LibMedia\lwjgl.jar"