Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getClass().getResource(...) returns null in a Jar!! help!

5,584 views
Skip to first unread message

Nils Olav Selåsdal

unread,
Jul 4, 2000, 3:00:00 AM7/4/00
to
I wanna pack my class files in a jar. I'm using som txt files and images,
as far as i can remember i used getClass().getResource("filename");
to retrieve data from a jar file.....
Ths works fine before i pack the class files in a jar, but when they're
stored ia a jar, the getClass() returns null!! help!
(using j2se v1.3 on WinNT).

[nOs]


Immo Benjes

unread,
Jul 4, 2000, 3:00:00 AM7/4/00
to

"Nils Olav Selåsdal" schrieb:

foo.bar.MyClass.getClass().getResource(filename) returns a URL to a file
foo.bar.filename

while foo.bar.MyClass.getClass().getResource("/"+filename)
would return an URL to filename without the packagename.

Immo


Nils Olav Selåsdal

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to

Immo Benjes <ben...@ifn.ing.tu-bs.de> wrote in message
news:39621CAF...@ifn.ing.tu-bs.de...

>
>
> "Nils Olav Selåsdal" schrieb:
>
> > I wanna pack my class files in a jar. I'm using som txt files and
images,
> > as far as i can remember i used getClass().getResource("filename");
> > to retrieve data from a jar file.....
> > Ths works fine before i pack the class files in a jar, but when they're
> > stored ia a jar, the getClass() returns null!! help!
> > (using j2se v1.3 on WinNT).
> >
> > [nOs]
>
> foo.bar.MyClass.getClass().getResource(filename) returns a URL to a file
> foo.bar.filename
so in my instance of foo.bar.MyClass, i should be able to say:
this.getClass().getResource(filename).
ok,?
this works fine before i pack my classes in a jar.(have all the directory
structure)
once foo.bar.MyClass is stored in a jar, the getClass() returns null ;-(

(i.e i get a nullpointerexception calling the getResource() on a "null"
object)

Jonathan Knight

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to
I had a similar problem when developing on NT which was caused by the
file names in a Jar being case sensitive. Everything worked fine before
I packed them up as NT is not case sensitive but once inside a Jar file
they were case sensitive.

Jonathan Knight
Senior Software Engineer
ARMATURE
Tel: 0113 2595247
Email JKn...@Armature.com


Michael Elizarov

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to
Also, if it's not a case issue try to use URLs. I've used this code to
load images from jar:

ImageIcon icon;
java.net.URL url;

// Specifying URL instead of file name is a little slower,
// but it works both from files and from .jar.
url = ClassLoader.getSystemResource("images/a2.gif");
icon = (url != null) ? new ImageIcon( url ) : null;

Relative path starts at the "top" of your jar.

Dirk Bosmans

unread,
Jul 6, 2000, 3:00:00 AM7/6/00
to
Not all JVM's can handle URL's with a 'JAR-file protocol'. I use my Applicet
Framework for properties files, audio and images with success, but haven't tried
any other resource types. See if it works for you.

I'm reacting to following parts of "Nils Olav Selåsdal" <nose...@frisurf.no>'s
article in comp.lang.java.programmer on Wed, 5 Jul 2000 08:06:11 +02006

.
. Immo Benjes <ben...@ifn.ing.tu-bs.de> wrote in message
. news:39621CAF...@ifn.ing.tu-bs.de...
. >
. >
. > "Nils Olav Selåsdal" schrieb:
. >
. > > I wanna pack my class files in a jar. I'm using som txt files and
. images,
. > > as far as i can remember i used getClass().getResource("filename");
. > > to retrieve data from a jar file.....
. > > Ths works fine before i pack the class files in a jar, but when they're
. > > stored ia a jar, the getClass() returns null!! help!
. > > (using j2se v1.3 on WinNT).
. > >
. > > [nOs]
. >
. > foo.bar.MyClass.getClass().getResource(filename) returns a URL to a file
. > foo.bar.filename
. so in my instance of foo.bar.MyClass, i should be able to say:
. this.getClass().getResource(filename).
. ok,?
. this works fine before i pack my classes in a jar.(have all the directory
. structure)
. once foo.bar.MyClass is stored in a jar, the getClass() returns null ;-(
.
. (i.e i get a nullpointerexception calling the getResource() on a "null"
. object)
.
.
. > while foo.bar.MyClass.getClass().getResource("/"+filename)
. > would return an URL to filename without the packagename.
. >
. > Immo
. >
. >
. >
.


Greetings,
Dirk Bosmans


http://users.belgacombusiness.net/arci/
- Applicet Framework: turns Applets into Applications
- ArciMath BigDecimal: now with BigDecimalFormat

Tarek Zeineddine

unread,
Aug 13, 2000, 3:00:00 AM8/13/00
to
hi,

Do i need to use the getClass() method if i want to use the .class fiels i
packed inside the jar file? I am trying to include some of the javax.swing
lasses into my jar, but i am not sure it is working...

thanks,

Tarek

0 new messages