Accessing resources in .jar

D

Dalibor Kusic

Hi!

I have 4 .txt files that I need to load data from during execution of
a Java program. I'm using method Class.getResourceAsStream() and it
works fine if the files are not packed inside a .jar file. Assoon as I
pack them inside a jar the method returns null...Why is that?
I made the .jar file without compression and it still persists...
 
T

Tony Morris

You'll have to load them as a path relative to the root of the jar file.
e.g.
getClass().getResourceAsStream("/directory/file.txt");

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
D

Dalibor Kusic

Are you sure that jar file is on the classpath ?

Yes....the application itself loads normally (classes), but returns an
IOException when trying to access the .txt files...
 
D

Dalibor Kusic

You'll have to load them as a path relative to the root of the jar file.
e.g.
getClass().getResourceAsStream("/directory/file.txt");

Well not neccessary....If I write just
getClass().getResourceAsStream("file.txt") the file must be in the
same directory hierarchy as the calling class...
i.e. calling class is myclass.new.class, then the upper call would
call /myclass/new/file.txt.

And once again I'm stressing out that everything works fine if I do
not pack the files in a JAR.

I came across a post mentioning a bug in the getResourceAsStream()
method which results in the method returning a null reference...but
that post was from 1998 or so....

and ideas?
 
T

Tony Morris

"Well not neccessary"

Yes, it is necessary.
You are also not providing a correct URI to your file (since it does not
begin with a '/')

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
B

Ben_

I came across a post mentioning a bug in the getResourceAsStream()
method which results in the method returning a null reference...but
that post was from 1998 or so....
Mmh, that reminds me that there have been problems, even under JDK 1.3.0,
IIRC.
What version are you using ?
 
T

Thomas Kellerer

Dalibor said:
Hi!

I have 4 .txt files that I need to load data from during execution of
a Java program. I'm using method Class.getResourceAsStream() and it
works fine if the files are not packed inside a .jar file. Assoon as I
pack them inside a jar the method returns null...Why is that?
I made the .jar file without compression and it still persists...

The jar file system is case sensitiv. I assume that you are using Windows which
does preserve the case but is not case sensitiv when opening files.

Check the spelling of your filename. If its name is "MyFile.txt", you *have* to
use the same name when calling getResourceAsStream().
getResourceAsStream("myfile.txt") will not work from a jar filesystem.

Thomas
 
D

Dalibor Kusic

"Well not neccessary"

Yes, it is necessary.
You are also not providing a correct URI to your file (since it does not
begin with a '/')

From Java documentation...

"This method delegates the call to its class loader, after making
these changes to the resource name: if the resource name starts with
"/", it is unchanged; otherwise, the package name is prepended to the
resource name after converting "." to "/". "

And I must stress out once again that everything seems to work fine
while everything is in it's original directory structure...As soon as
I pack files and classes in a .jar it cannot load the .txt files.
Anyway...Just to be on the safe side here is the latest code I used...

directorystructure:

- hr\fer\zemris\dk383610\turing\java.class
- data\file.txt

Inside java.class (names are just exampletory) there is a call

this.getClass().getResourceAsStream("/data/file.txt");

This works just fine until I pack both directories in a JAR with, for
example, the following command...

jar cf0 TuringApplet.jar hr data

After that the applet loads (classes do load), but the .txt files do
not....
 
D

Dalibor Kusic

The jar file system is case sensitiv. I assume that you are using Windows which
does preserve the case but is not case sensitiv when opening files.

Check the spelling of your filename. If its name is "MyFile.txt", you *have* to
use the same name when calling getResourceAsStream().
getResourceAsStream("myfile.txt") will not work from a jar filesystem.

Yes I thought of that before but as far as I can see I use the same
casing everywhere in my code and in my file system....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top