Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File can't be found?  RSS feed

 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm currently trying to make a game in java, and for that i need to import some .png pictures that i made in paint.

To do this I do the following:



It kept throwing exceptions evrytime i ran the program, no matter what i did with the code or picture.

I found out that it was the File that could not find the picture, even though it is located right in the same folder!?

I have been using the exact same code other places in the game (where it works), so it's sort of strange to me that this isn't?

Does anyone have a suggestion on what im doing wrong?
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean the same folder as your class? Then you should use Class.getResourceAsStream, not File.
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the picure and the class are located in the same folder.

Im not familiar with the Class.getResourceAsStream thing, do you mind giving a breif explaination on how I use it?

It would be very helpful
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oddly enough there isn't a dedicated section for resources in the Java Tutorial. I did manage to find a section in How to Use Icons though. This example uses getResource; getResourceAsStream works 100% the same except it returns an InputStream instead of a URL.

In fact, getResourceAsStream(x) returns getResource(x).openStream(), or null if getResource(x) would return null.
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot! It now works just fine

And I found out that the main problem was (and this is VERY embarrassing) I tryed to add the image to a not yet initialized arraylist :P

But thanks a lot for the help
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I've reach a point where I'd like to show my friends how far I got, sp I'd like to make a simpe executable jar file.

Not that difficult but I can't figure out how I can make the program read files from inside the .jar .

I have all the classes packed with a folder containing images and audio.

How can I tell the program to read data from the folder in the jar, instead of looking around it?
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You make sure that the .jar is part of your classpath, then you use the Class.getResourceAsStream method to read a resource from inside the jar.
 
Javad Rashidi
Ranch Hand
Posts: 34
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this method can help you

parent: component that want load file to it.
path: path of file from root of jar file.
this method convert the path of file to URL that is very usefull for me.
here is an example
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm.. I'm aware of the "let them do thier own homework" policy, and this might be a disapointing result of my progress, but I have tried just about all the different getRecouse and classLoader statements i could find on google.

The result is always eather not reding any data at all, or not reading data when inside the .jar.

so I was wondering if anyone could tell me how I might get it working from where I am now, that is:

The statement for loading data:



when packing a jar:



and the mainfest:


If anyone have a suggestion, please tell me
 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, kinda looks like a dead end.

If we can take it from top?

Can someone tell step by step: How would you make an executable .jar (that can run from enywhere) that when it's run, it will bring up a JFrame with a pucture inside the frame that is saved within the jar with the class?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!