Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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  
  urgent guidance required to access image using class navigation  (Read 1889 times)
0 Members and 1 Guest are viewing this topic.
Offline dav0012

Senior Newbie





« Posted 2014-05-18 15:19:08 »

hello , in the program that i am developping, i have to access a list of files within the package of the project. it runs well when i run the program within eclipse but when i export it to a runnable jar, it seems that the path for my image folder changes.
From research i ended with solutions like using Class.getResource
or get ResourceStream
 which i have never used before and it seems that i am not being able to implement it.

plz its urgent help my image folder and Class(which call the images) is as follows.
followed by my code extract.





plzz help me out, i got to submit an executable jar in less than 5 hours
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #1 - Posted 2014-05-18 15:30:25 »

Can't use File() when getting stuff from inside a jar, as the jar is actually a zip, and the "files" inside aren't actually files.
You have to work with InputStreams:

1  
buffImg = ImageIO.read(ThumbnailsList.class.getClassLoader().getResourceAsStream("jp/images/thing.png"));
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2014-05-18 15:30:40 »

Don't use Files, if your resource is not in a File.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dav0012

Senior Newbie





« Reply #3 - Posted 2014-05-18 15:42:07 »

Can't use File() when getting stuff from inside a jar, as the jar is actually a zip, and the "files" inside aren't actually files.
You have to work with InputStreams:

1  
buffImg = ImageIO.read(ThumbnailsList.class.getClassLoader().getResourceAsStream("jp/images/thing.png"));


okay how to access all the images in the folder(or package) then.. i dont really knw how to access ALL the images from the folder.
 my next instruction is to loop over all the images and add them in an array.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2014-05-18 15:46:11 »

either hardcode the filenames, or create a pattern to your file names:

1  
2  
3  
4  
5  
jp/images/sprite-1.png
jp/images/sprite-2.png
jp/images/sprite-3.png
jp/images/sprite-4.png
jp/images/sprite-5.png


1  
2  
3  
4  
for(int i=0; i<5; i++) {
   String path = "jp/images/sprite-"+(i+1)+".png";
   buffImgs[i] = ImageIO.read(ThumbnailsList.class.getClassLoader().getResourceAsStream(path));
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline dav0012

Senior Newbie





« Reply #5 - Posted 2014-05-18 17:07:45 »

okay thanks Smiley
i take it that we cannot do it in a dynamically way.. (without knowing the file names)

i think i'll save the filenames in a text file and then read it through the stream. and recover the images.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2014-05-18 17:16:41 »

You can do it, but you'll have to iterate over the contents of a JAR/ZIP, with ZipInputSteam

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (581 views)
2019-09-04 15:33:17

hadezbladez (5510 views)
2018-11-16 13:46:03

hadezbladez (2402 views)
2018-11-16 13:41:33

hadezbladez (5772 views)
2018-11-16 13:35:35

hadezbladez (1223 views)
2018-11-16 13:32:03

EgonOlsen (4661 views)
2018-06-10 19:43:48

EgonOlsen (5682 views)
2018-06-10 19:43:44

EgonOlsen (3198 views)
2018-06-10 19:43:20

DesertCoockie (4095 views)
2018-05-13 18:23:11

nelsongames (5115 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!