Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  FAQ: cannot find the resource/file  (Read 2175 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2014-05-18 16:37:45 »

Having trouble finding your resources when your exported your files to a JAR?

Step 1: never use
File
or
FileInputStream
to lookup resources in a JAR


Didn't that quite solve your problem?

Step 2: put this code in your application:


Now it's time to figure out why your resource cannot be found:

Step 3: insert this line where error occurs
1  
ClassLoaderDummy.searchResource("res/images/sprite.jpg");






Example input:
1  
2  
3  
4  
5  
6  
7  
8  
9  
   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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
[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"

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline tom_mai78101
« Reply #1 - Posted 2014-05-19 11:40:22 »

Question:

Should one use
ClassLoaderDummy.class.getResource()
,
ClassLoaderDummy.class.getClassLoader().getResource()
,
ClassLoaderDummy.class.getResourceAsStream()
, or
ClassLoaderDummy.class.getClassLoader().getResourceAsStream()
?
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2014-05-19 12:00:10 »

1  
2  
[ClassLoaderDummy]          for access use:
[ClassLoaderDummy]                getResourceAsStream("/res/images/Sprite.JPG");

As the suggested path starts with a "/", it doesn't really matter how you call
getResourceAsStream(path)


These will (for 99.9% of configurations) all yield the same result:
1  
2  
3  
4  
5  
String path = "/res/anything/sprite.jpg";
InputStream in1 = MyClass.class.getResourceAsStream(path);
InputStream in2 = myInstance.getClass().getResourceAsStream(path);
InputStream in3 = myInstance.getClass().getClassLoader().getResourceAsStream(path);
InputStream in4 = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bogieman987
« Reply #3 - Posted 2014-05-28 14:48:55 »

Quote
Step 1: Never use File or FileInputStream to lookup resources in a JAR

But otherwise otherwise it's perfectly fine? Just not "to lookup resources in a JAR".
Also, why is that?

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #4 - Posted 2014-05-28 15:32:27 »

Yes, and because the jar is a zip file, the computer can't read normally through the file system. Files packed in a jar needs a special stream to read the data within.

Offline bogieman987
« Reply #5 - Posted 2014-05-28 15:59:56 »

Ah, I see, thanks.

Offline AppleSauce
« Reply #6 - Posted 2014-05-28 20:43:26 »

I loaded a texture based on what you said:
1  
      TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/images/sprite.jpg"));


The exception was :

1  
2  
3  
Exception in thread "main" java.lang.RuntimeException: Resource not found: res/images/sprite.jpg
   at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
   at imgtest.main(imgtest.java:12)
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2014-05-28 20:45:51 »

I loaded a texture based on what you said:
1  
      TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/images/sprite.jpg"));

My ClassLoaderDummy code always outputs paths starting with "/".

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline AppleSauce
« Reply #8 - Posted 2014-05-28 20:47:50 »

My bad, I didn't follow Step 2.  Undecided
Offline AppleSauce
« Reply #9 - Posted 2014-05-28 21:04:11 »

Now that I got that setup, your class tells me

1  
2  
3  
4  
5  
6  
7  
[ClassLoaderDummy] SEARCHING: "res/space.png"
[ClassLoaderDummy] FOUND: mislocated resource:
[ClassLoaderDummy]                "space.png"
[ClassLoaderDummy]          in classpath entry:
[ClassLoaderDummy]                "C:\Users\Owner\workspace\FlubberSpace\bin"
[ClassLoaderDummy]          for access use:
[ClassLoaderDummy]                getResourceAsStream("/space.png");


and I did use getResourceAsStream when I accessed it with the texture loader.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2014-05-28 21:16:39 »

So... it works... right?


If not, what are you testing?
1. probably loading the resource from within Eclipse (does it work?)
2. loading the resource when launching a jar (does it work?)

If the answers are: 1. yes, 2. no, then run ClassLoaderDummy from the jar, and let it find the resource from *that* classpath.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline AppleSauce
« Reply #11 - Posted 2014-05-28 21:38:43 »

It works fine in eclipse, just not when I export it. 2 is what I'm aiming for but It gives the same results.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2014-05-28 21:53:48 »

It works fine in eclipse, just not when I export it. 2 is what I'm aiming for but It gives the same results.


So, the same? ...when you run it from the jar, you get this?

1  
2  
3  
4  
5  
6  
7  
[ClassLoaderDummy] SEARCHING: "res/space.png"
[ClassLoaderDummy] FOUND: mislocated resource:
[ClassLoaderDummy]                "space.png"
[ClassLoaderDummy]          in classpath entry:
[ClassLoaderDummy]                "C:\Users\Owner\workspace\FlubberSpace\bin"
[ClassLoaderDummy]          for access use:
[ClassLoaderDummy]                getResourceAsStream("/space.png");

Really??


Did you read this carefully?
If the answers are: 1. yes, 2. no, then run ClassLoaderDummy from the jar, and let it find the resource from *that* classpath.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline AppleSauce
« Reply #13 - Posted 2014-05-28 22:19:02 »

I don't think I understand how to do that. Do I call the ClassLoaderDummy to searchfor it  and export the jar with that class?
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2014-05-28 22:19:58 »

yes

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline AppleSauce
« Reply #15 - Posted 2014-05-28 23:21:43 »

It still doesn't work. This is how I export it.

Export >> Runnable Jar >> COpy Libs to a folder near the jar >> finish >> open Jarsplice >> Add jar and jars from lib folder >> add natives >> input main class >>> create fat jar >> cmd can't load resource

Should I just create a folder in the cdrive with all the  images?
Offline AppleSauce
« Reply #16 - Posted 2014-05-29 00:10:29 »

Ah, maybe It was my gameloop I think. the old flubberspace works fine. Hmm..
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2014-05-30 15:11:59 »

You have to run ClassLoaderDummy just prior to the line that attempts to load the resource.

Then run the program from the exported JAR, and post the output here (if you can't resolve the issue yourself).

N.B.: the output you post must contain output from ClassLoaderDummy.







Old code example:
1  
TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/images/sprite.jpg"));


New code example:
1  
2  
3  
String path = "res/images/sprite.jpg";
ClassLoaderDummy.searchResource(path);
TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(path));


Output example you would post here:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
[ClassLoaderDummy] SEARCHING: "res/images/sprite.jpg"
[ClassLoaderDummy] FOUND: .......... resource:
[ClassLoaderDummy]                ......
[ClassLoaderDummy]          in classpath entry:
[ClassLoaderDummy]                ......
[ClassLoaderDummy]          for access use:
[ClassLoaderDummy]                getResourceAsStream(..........);

Exception in thread "main" java.lang.RuntimeException: Resource not found: res/images/sprite.jpg
   at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
   at imgtest.main(imgtest.java:12)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (35 views)
2014-12-09 22:41:13

BurntPizza (70 views)
2014-12-08 04:46:31

JscottyBieshaar (32 views)
2014-12-05 12:39:02

SHC (44 views)
2014-12-03 16:27:13

CopyableCougar4 (40 views)
2014-11-29 21:32:03

toopeicgaming1999 (108 views)
2014-11-26 15:22:04

toopeicgaming1999 (94 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!