Help the Noob, Jar Files

Ok, first of all, YES I know how to make jar files in eclipse. I have made sucessful jar files with several programs, like kev glass’ tutorials into jar form.

BUT, I am having issue with packing my program. My Hex Map program, is somewhat based off of the kev glass tut, but when I pack mine, and i excecute it through the command promt, so i can see what it prints, it prints some of my debug variables, then it says it cant find the graphic references, as i stated it should do in the program if it doesnt find the .pngs. YES, i thought that i included all of my graphics and resources in the program, but when i click my jar NOTHING happens, just those few prints in the command promt.

Heres how i handel images:

public Graphic getGraphic(String ref, String type) {
		
		// if we've already got the sprite in the cache
		// then just return the existing version
		if (graphics.get(ref) != null) {
			return (Graphic) graphics.get(ref);
		}
		
		// otherwise, go away and grab the sprite from the resource
		// loader
		BufferedImage sourceImage = null;
		
		try {
			// The ClassLoader.getResource() ensures we get the sprite
			// from the appropriate place, this helps with deploying the game
			// with things like webstart. You could equally do a file look
			// up here.
			URL url = this.getClass().getClassLoader().getResource(ref);
			
			if (url == null) {
				fail("Can't find ref: "+ref);
			}
			
			// use ImageIO to read the image in
			sourceImage = ImageIO.read(url);
		} catch (IOException e) {
			fail("Failed to load: "+ref);
		}
		
		// create an accelerated image of the right size to store our sprite in
		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		
		/** Keenan's Renderer Mod */
		if (type=="oGRAPHIC") { 
			image = gc.createCompatibleImage(sourceImage.getWidth(),
					                         sourceImage.getHeight(),Transparency.OPAQUE);
		}
		else if (type=="tGRAPHIC") { 
			image = gc.createCompatibleImage(sourceImage.getWidth(),
					                         sourceImage.getHeight(),Transparency.TRANSLUCENT);
		}
		
		// draw our source image into the accelerated image
		image.getGraphics().drawImage(sourceImage,0,0,null);
		
		// create a sprite, add it the cache then return it
		Graphic graphic = new Graphic(image);
		graphics.put(ref,graphic);
		
		return graphic;
	}

I have loaded my images like this:


if      (TEXTURE == GRASSLAND) { 
			return GraphicStore.get().getGraphic(
				   "\\ART\\TERRAIN\\GRASSLAND\\hex_green.png","tGRAPHIC");
		}

“\ART\TERRAIN\GRASSLAND\hex_green.png”

Well, those are backslashes. ::slight_smile:

so? it excecutes perfectly in the ide, with pictures, its supposed to be backslash?

\ = backslash (a windows thing)
/ = slash (works everywhere)
URL = something with slashes
broken URL = something with backslashes

sp, i replaced it with:

“/ART/TERRAIN/OCEAN/hex_blue.png”,“tGRAPHIC”);

and it cant find ref in ide so it doesnt work!

and it cant find ref in ide so it doesnt work!

Then there is no “/ART/TERRAIN/OCEAN/hex_blue.png” in any jar/dir in the classpath.


http://kaioa.com/k/jarimage.jar

Source included.

there is no source with that file.

There is.

well i aint know how to get it, all i get is a jar

this will blow your mind:

ready?

jars are zip files.

boom! now have at it :slight_smile:

man, im an idiot i got it now.

Problem is i have loaded my images just like you with the imageIO reader thingy, but i use a class.

Yet when i switch the “//” to “/” it cant fine the ref, but heres a pick of my directory with my profram, and i use a package, and ART folder is where my images are.

http://img390.imageshack.us/img390/1412/help1ua9.png

I use this in quote to load for my class with the IO reader:
“/ART/TERRAIN/GRASSLAND/hex_green.png”

and it doesnt work when its not “//”

again I don’t know what is the problem, here’s how I do it:


...
BufferedImage game_paused_bfimg = loadAndCreateCompatibleImage("images/game_paused.png", BufferedImage.TRANSLUCENT);
...

    /** load image from PATH and make it compatible so it can be accelerated
     * @author Kova (kova1337@gmail.com)
     * @param path - relative path to image, ex.: "images/some_image.png"
     * @param image_type - type of BufferedImage
     * @return compatible loaded BufferedImage or null if reading fails
     * @see Transparency.OPAQUE, .BITMASK, .TRANSLUCENT
     */
	public BufferedImage loadAndCreateCompatibleImage(String path, int image_type) {
            URL url = null;
	    try {
	    	url = this.getClass().getClassLoader().getResource(path);
                GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
                BufferedImage temp1_bfimg = ImageIO.read(url);
                BufferedImage temp2_bfimg = gc.createCompatibleImage(temp1_bfimg.getWidth(), temp1_bfimg.getHeight(), image_type);
                temp2_bfimg.getGraphics().drawImage(temp1_bfimg, 0, 0, null);
	        return temp2_bfimg;
	    } catch (Exception e) {
	    	System.out.println("Error loading image: " + path + " " + url);
	    	System.exit(0);
	    	return null;
	    }
	}

If I understand correctly, you have packaged your ART folder in a jar, right?

  1. Is the jar in your classpath when you start your program?
  2. Did you check if the contents of your jar indeed includes that paths you expect?
  3. Did you check the capitalization of the paths?

You definitely need forward slashes and no backslashes. Back slashes might work on the windows filesystem, but it won’t anywhere else.
You could also try to skip the first (forward!) slash.

[size=10pt]WOW[/size]

IT WORKED!!!

ALL I had to do was this instead:
“ART/TERRAIN/GRASSLAND/hex_green.png”

NO SLASH in BEGINNING! Before, I tried it, I had a “/” at the beginning and it failed. Now my jar WORKS!!

Thank you guys!!