• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

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.
0
Phantomas090198
Asked:
Phantomas090198
1 Solution
 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now