Re: reading GIFs from a JAR file

Rick Fincher <[email protected]>
Newsgroups gmane.org.user-groups.trijug.juglist
Message-ID <[email protected]>
Another solution that works both jarred and un-jarred is to use a URL 
instead of a file path, like so:

URL url = Main.class.getResource("/view/gifs/DNA18x28.gif"); // must 
have initial slash
ImageIcon DNA_ICON = new ImageIcon(url);

This assumes that the name of the class loading the image is called 
"Main", rename as necessary.

Richard, my previous post was incorrect, sorry for the confusion.  My 
code was actually picking up the image from a folder outside the jar.

With this example the "/view/gifs" directory does not even have to be in 
the manifest's class-path.  The directory just needs to be in the same 
jar file as the code that loads the icon.

Note that the initial slash indicates that the path: 
"/view/gifs/DNA18x28.gif" starts at the root of the jar.  If you put the 
directory somewhere else you may hve to refer to it relative to the 
loading class.

In this case the executable jar has a folder with the class "Main" at 
the root, and a folder called "view" at the same level.

Using URL's also has the other standard advantages.

Rick

cebmailbox-trijug-/[email protected] wrote:

> I find this always works, jar or no jar:
>  
> ImageIcon DNA_ICON = new ImageIcon(
>             Object.class.getResource("/view/gifs/DNA18x28.gif"));
>  
> Note:
> 1.
> If this call is in an instance method feel free to replace 
> "Object.class" with "this.getClass()".
> 2.
> Note that graphic file can now be "absolutely" pathed if you start the 
> path with "/", or relatively pathed if "view" is a sub-directory 
> (sub-package) of the class where this code is located.
>  
> For instance if this code is in an instance method of a class in the 
> package:
>   test.gui
> and the graphic is in:
>   test.gui.view.gifs
> then you may do:
>  
> ImageIcon DNA_ICON = new ImageIcon(
>             this.getClass().getResource("view/gifs/DNA18x28.gif"));
>  
>  
> -Chris
>
> */Richard Hammer <[email protected]>/* wrote:
>
>     I am trying to read GIF files from a JAR. The GIFs are in a
>     subdirectory in the JAR. This JAR also contains our class files in
>     other subdirectories. This JAR is in the classpath, and
>     classloader is
>     finding the class files.
>
>     But the GIFs are not being found. I had thought I would be able to
>     read
>     the GIF files with code like this:
>     ImageIcon DNA_ICON = new ImageIcon("view/gifs/DNA18x28.gif");
>
>     where "view/gifs/DNA18x28.gif" is the path of the GIF in the JAR.
>     But this is not working for me.
>
>     What I find with Google suggests that I may need to open a
>     java.util.jar.JarInputStream, from which I can read JarEntry(s) while
>     looking for my specific GIF file. Is it really that hard? Is there an
>     easier way?
>
>     Thanks,
>     Rich Hammer
>
>     _______________________________________________
>     Juglist mailing list
>     [email protected]
>     http://trijug.org/mailman/listinfo/juglist_trijug.org
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Juglist mailing list
>[email protected]
>http://trijug.org/mailman/listinfo/juglist_trijug.org
>  
>

_______________________________________________
Juglist mailing list
[email protected]
http://trijug.org/mailman/listinfo/juglist_trijug.org
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.