Searching for image files inside a jar

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:

@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 assfuck){
			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 :slight_smile:

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);
	}
	
}

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

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

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.

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.


throw new IOException(assfuck);

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. 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 :stuck_out_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

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 :slight_smile: