Results 1 to 5 of 5

Thread: Bundling images in a JAR file and speed problems

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    Bundling images in a JAR file and speed problems

    I'm trying to deploy an application I've written in a JAR file. Part of the application loads in images to use as button icons. However, when I specify the path to the images I have to specify some kind of absolute path like c:\images\image.gif or /images/image.gif. That isn't an option for the JAR file version where the image files are inside of it. I tried using images/image.gif but it didn't work.

    Also, my application is producing a PDF file after reading data through an ODBC connection. If I run the application by itself then all is well. If I run it after it's been put in the JAR file, the application works but takes about ten times as long to run.

    Any ideas?
    Last edited by Holywhippet; Jul 4th, 2005 at 12:07 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Bundling images in a JAR file and speed problems

    Use the class loader's getRessource function, it can load from within a JAR file.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    Re: Bundling images in a JAR file and speed problems

    I've tried that. I've been following the examples here: http://java.sun.com/docs/books/tutor...misc/icon.html

    None of them work for me. I've got the right version of the JRE but it just won't load the images no matter what.

  4. #4
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: Bundling images in a JAR file and speed problems

    Here is code that will attemot to load from an external file, then a JAR'd file if it can't be found in the first case, one issue we've had with it is that repeatedly loading teh images is time consuming, adding a weak hashmap to store the name vs image in means you can store them all and retreive them easier next time...

    Code:
    	private static BufferedImage loadImage(String name) {
    		InputStream in;
    	     
    		// First try the classloader
    		in = ImageLoader.class.getResourceAsStream(name); 
    	  
    		// check to see 
    		if(in == null) {
    			// Are we instead looking at a filename?
    			try {
    				FileInputStream fis = new FileInputStream(name);
    		    // 	It's open!
    				in = fis;
    			} catch(FileNotFoundException foe) {
    		    // 	we give up!
    				return null;
    			}
    		}
    		 
    		if(in != null) {
    			try {
    				try {
    					return ImageIO.read(in);	
    				} finally {
    					in.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		return null;
    	}

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    Re: Bundling images in a JAR file and speed problems

    Thanks that code works fine for me. The images aren't loaded very often and there are only about five of them so I won't need a hashmap.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width