Extracting a gif file from the application jar

I built a jar file containing all the class files, properties and gifs of the application.
The application cannot load the gifs.
Is there a special way to do it?

Everything is ok when running without building the jar.
Phantomas090198Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sen_kumCommented:
 
  To load the gif from jar in an application you must do the following

try {
  MediaTracker m = new MediaTracker(this);
  InputStream is = getClass().getResourceAsStream("Image.gif");
  BufferedInputStream bis = new BufferedInputStream(is);
  byte[] bytes = new byte[10000];
  int byteRead = bis.read(bytes,0,10000);
  img =   Toolkit.getDefaultToolkit().createImage(bytes,0,byteRead);
  m.addImage(img, 0);
  m.waitForAll();
  }
 catch(Exception e) {
  e.printStackTrace();
  }

    Because of some security reason, it's not possible with some browser (like Netscape) to use the getResource() method from an Applet. Instead we must use the getResourceAsStream method.
   
try {
  MediaTracker m = new MediaTracker(this);
  InputStream is =   getClass().getResourceAsStream("Image.gif");
  BufferedInputStream bis = new BufferedInputStream(is);
  byte[] bytes = new byte[10000];
  int byteRead = bis.read(bytes,0,10000);
  img = Toolkit.getDefaultToolkit().createImage(bytes,0,byteRead);
  m.addImage(img, 0);
  m.waitForAll();
  }
 catch(Exception e) {
  e.printStackTrace();
  }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JodCommented:
From the Java FAQ, some points to note are:

1) jpgStream.available() (wrongly) returns 1 when .gif files in the .jar file are compressed! Some people now create a .jar file in two steps, essentially:

      jar cvf x.jar *.class
      jar uf0 x.jar *.gif

This ensures .gifs are not compressed, and hence that available() doesn't lie to you. David Alex Lamb stumbled onto this after wondering why his .gif files looked bad, then realized that compression of a .gif might throw away detail. The conjecture is that available() returns 1 because it has to block to do uncompression.

2) There is now a bug that prevents retrieving files from jars, from working in JDK 1.2.2. See http://developer.java.sun.com/developer/bugParade/bugs/4251549.html 


In general, there are two ways to specify resources.

using an absolute path name, such as "/myPackage/resourcedir/myResource.txt"
With an absolute path name, the resource file is actually relative to the classpath.
using a relative path such as "dir/myResource.txt".
If you specify a relative path, then the file is found relative to where the class loader found the package of the Class that is loading the resource. The consequence of this is that relative resource files can only be in the same directory as your class file or in a directory below.
If you want to access resources from an unsigned applet, use relative resources.
There are a couple of methods to get at resources, including methods in the java.lang.Class and java.lang.Classloader. It uses the class loader because that code knew where to find the class on the filesystem. If the resource file is nearby, it can be found the same way. One simple method to get a stream for a resource for a particular class, say Mypackage.MyClass, is as follows:

String relativePath = "resourceDir/somefile.txt";
String absolutePath = "/somePackage/somefile.txt";
InputStream in=Mypackage.MyClass.class.getResourceAsStream(relativePath);

If the class cannot be loaded, "in" would be assigned a null value. Otherwise, you can use the inputStream just as any other.
A brief note about the syntax used above. In java 1.1 and above, a java.lang.Class object for a particular class can be acquired by appending .class to the class's name. Though it looks like every object has a static member variable, this is not actually the case.

When using resources with Netscape, be aware of the Netscape restrictions that:

Netscape does not implement the getResource() methods in java.lang.Class and java.lang.ClassLoader, but only getResourceAsStream() methods.
All resources must be in a Jar/archive file
Resource files must have a Netscape-approved extension, or you must call certain functions before hand. See the following for details: http://developer.netscape.com/docs/technote/java/getresource/getresource.html 
Internet Explorer does not seem to have these restrictions.



Finally, a lengthy example of using gifs from Java files...

http://www.javaworld.com/javatips/jw-javatip49.html

0
Ravindra76Commented:

It's fine sen_kum and Jod
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.