Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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  
  Searching for image files inside a jar  (Read 1408 times)
0 Members and 1 Guest are viewing this topic.
Offline Kurten

Junior Devvie


Medals: 3
Exp: 3 years


-w-


« Posted 2012-02-22 14:28:37 »

Hello folks!

I have a problem in my code. I want to read image files from within a Jar, or to be more precise, I want to count them.

I can load images from the loadImage() method I have created, looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
@Override
   public Image loadImage(String path) throws IOException{
      if(Mothership.infoActive()){System.out.println("Loading image: " + path);}
      Image tempImg;
      try{
         tempImg = new ImageIcon(getClass().getResource(path)).getImage();
         if(tempImg != null){
            if(Mothership.infoActive()){System.out.println("Image " + path + " loaded without errors");}
            return tempImg;
         }else{
            return null;
         }
      }catch(NullPointerException assf**k){
         throw new IOException();
      }
   }


Now, I want to scan the folders inside the jar file to see how many images are in there so that I can build my program to adapt itself to how many images there are in each folder. I found this piece of code on stack overflow which does the trick, but with one small problem. It won't let me run the program inside eclipse, it doesn't find the images if they are not packed in a Jar. I am not very good at file IO so help and advice would be greatly appreciated Smiley

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  
public class Main {
   
   String[] lol;
   CodeSource src = Main.class.getProtectionDomain().getCodeSource();
   List<String> list = new ArrayList<String>();
   
   public static void main(String[] args){
      Main m = new Main();
      m.init();
   }
   
   public void init(){      
      try {
         read();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
   
   
   
   public void read() throws IOException{
      if(src != null){
         System.out.println("Check");
         URL jar = src.getLocation();
          ZipInputStream zip = new ZipInputStream( jar.openStream());
          ZipEntry ze = null;
         
          while((ze = zip.getNextEntry()) != null){
              String entryName = ze.getName();
              if(entryName.endsWith(".png") || entryName.endsWith(".PNG") ) {
                  list.add(entryName );
                  System.out.println(entryName);
              }
          }

      }
      lol = list.toArray( new String[ list.size() ] );
      System.out.println(lol.length);
   }
   
}
Offline tom
« Reply #1 - Posted 2012-02-22 15:00:49 »

http://www.rgagnon.com/javadetails/java-0665.html

Although I would consider not storing the files in the classpath. Put it in folder or zip file. It's much easier.

Offline Stranger

Senior Devvie


Medals: 9



« Reply #2 - Posted 2012-02-22 15:38:33 »

You can use ZipFile.entries() to iterate archive.

Anton
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-02-22 16:53:13 »

Or just put them in a zip file in your classpath.  Honestly, why make your job harder?  I feel like we do nothing but reinvent wheels and give them square edges here.
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-02-22 17:09:28 »

As an aside, you are throwing an IOException when a NullPointerException is caught and you completely ignore it. Instead, pass the exception as a parameter to the IOException constructor.
1  
throw new IOException(assf**k);

Offline Kurten

Junior Devvie


Medals: 3
Exp: 3 years


-w-


« Reply #5 - Posted 2012-02-22 19:38:46 »

Im not sure if I have expressed what I want to do very good. I want to pack everything in one jar file, why do this? Because this program is meant for people having trouble with math and computers. It's educational software and I want it to be portable and easy to use. What I want to do is count how many images are stored inside the image folders inside the jar file. Like this:

1  
2  
3  
4  
1. Iterate the image folders inside the jar file this code executes from
2. count how many images are in thisjarfile/images/geometry/ and make an image array with that many elements
3. make the same thing with algebra, calculus, arithmetic etc
4. load the images and put them into the arrays created in step 2


The code I submitted can do that if you compile it and pack it into a jar then executes the jar like a regular java application, but when you try to run it inside eclipse to debug for example it doesn't find any images at all which makes my program crash. Why can't it find the images?

@Ra4king: I did that to express my hate for nullpointers, it's a little joke actually Tongue I will fix it when I make the code ready for release.

@Sproingie: How do you put a zip in the classpath? O_o And how exactly do I make square wheels? I only asked a question xD
Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2012-02-22 20:48:15 »

@Sproingie: How do you put a zip in the classpath? O_o And how exactly do I make square wheels? I only asked a question xD

My answer was facetious: a jar file is a zip file, so you add a jar to the classpath.  Actually java will cope with a .zip extension just fine too, for legacy reasons, but you really shouldn't do that.  

Anyway, as the one asking how best to roll, you're not the one inventing the wheels -- my comment was directed at people recommending the more brittle solutions depending on just the filesystem or just zipfiles, when there's already a perfectly good resource loader that doesn't care which one you use (and also handles HTTP if you deploy with applets or webstart).  As for your other question, you can have square wheels if your road is sinusoidal Smiley


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.

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

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

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
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!