Re: Accessing files within JAR archive

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 10/30/03


Date: Thu, 30 Oct 2003 11:52:32 GMT

Bert,

"Bert Sierra" <bert_sierra@mac.com> wrote in message
news:bert_sierra-BD7D75.02300830102003@news.commspeed.net...
> Howdy --
>
> I'm fairly new to Java, so I'm easily stumped. The question
> is: How do I access a file within a JAR archive from a Java
> program?
>
> I have a simple test program, shown below, which illustrates
> the problem:
>
> import java.io.*;
> import java.util.*;
>
> public class FileTest {
>
> public static void main (String args[]) {
> File file = new File("test.gif");
> System.out.println("exists = " + file.exists());
> System.out.println("length = " + file.length());
> }
>
> }
>
> If I store the test.gif file outside the JAR archive, in the
> same directory as the JAR archive, everything runs fine -- the
> file exists and I get the proper length. If I store the test.gif file
> in the JAR archive, it can't be found and length()==0. None of
> the Java texts I have on hand seem to address this issue.
>

Which is exactly what *should* happen !

A '.jar' file is, essentially, a '.zip' file. Its contents is a collection
of bytes, not files. Certainly some of those bytes represent
previously-stored files, and can be 'extracted' to recreate facsimiles of
those files. However, until that is done, no 'files' can be said to exist.

So, to treat 'test.gif' as a standalone entity - a file - store it as such.
On the other hand, if you want to keep such resources in the '.jar' file and
extract when needed, use:

* 'Class.getResource' to extract the file as an URL
* 'Class.getResourceAsStream' to extract the contents of the
   'file' as a byte stream

Check out: http://mindprod.com/jgloss/jar.html for more information.

I hope this helps.

Anthony Borla

P.S.

I knew great rock music came out of Canada [e.g. Rush] but now know of a
great Java-related web site - mindprod.com. A terrifically useful site,
Roedy - well done :) !



Relevant Pages

  • Re: ZIP component that does direct file I/O to a file in archive
    ... Java doesn't run any background thread to first extract the entire ... >>The Delphi components that I have looked at allows dealing with memory ... > down into a load of smaller files - or doing the compression yourself ...
    (alt.comp.lang.borland-delphi)
  • Re: ZIP component that does direct file I/O to a file in archive
    ... or implied) to jump into the middle of the archived file and ... I brought the filesize into the equation only to highlight the ... I know that because, if it were so, Java, being slower, would take ... >even more time to extract the file, ...
    (alt.comp.lang.borland-delphi)
  • Re: program that surfs through site with javascript...
    ... I think you can get the links since I doubt JavaScript is being used ... interpreter to fake what the browser does. ... cut and then paste into Java. ... Java script and write the equivalent Java to extract what you want. ...
    (comp.lang.java.programmer)
  • Re: Retrieve version number for Windows executable
    ... > Is there a way to extract the version number of a Windows executable ... > file using Java? ... I looked at a few of my 'exe' files and they don't all have this info. ... Prev by Date: ...
    (comp.lang.java.programmer)
  • Re: How to save user inputs?
    ... If you want to deploy it using webstart, ... Since 1.4, Java can read and write XML, a better ... way to store more complicated data than a Properties ... complicates everything). ...
    (comp.lang.java.help)