Ouabaine, 15.10.2007 16:32:
> Hello all,
>
[quoted text clipped - 4 lines]
> you give me a path to the classes and methods dealing with resources in jar
> files?
Classloader.getResourceAsStream() or Class.getResourceAsStream() both
return an InputStream which you can use to read the file.
The first one allows you to specify the location inside the jar file,
the second one assumes the "resource" file is located in the same
package (i.e. directory) where the .class file is located.
Thomas
Lew - 15 Oct 2007 15:46 GMT
> Classloader.getResourceAsStream() or Class.getResourceAsStream() both
> return an InputStream which you can use to read the file.
>
> The first one allows you to specify the location inside the jar file,
> the second one assumes the "resource" file is located in the same
> package (i.e. directory) where the .class file is located.
Not true. The two are exactly equivalent, in fact, the Class version invokes
the ClassLoader version.
<http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(j
ava.lang.String)>
> This method delegates to this object's class loader.

Signature
Lew
Jean-Baptiste Nizet - 15 Oct 2007 15:59 GMT
> > Classloader.getResourceAsStream() or Class.getResourceAsStream() both
> > return an InputStream which you can use to read the file.
[quoted text clipped - 9 lines]
>
> > This method delegates to this object's class loader.
And, if you read the remaining of the documentation, you'll find this:
Before delegation, an absolute resource name is constructed from the
given resource name using this algorithm:
* If the name begins with a '/' ('\u002f'), then the absolute name
of the resource is the portion of the name following the '/'.
* Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this
object with '/' substituted for '.' ('\u002e').
This means that if the file is stored in the same package/directory as
the class, you mya just use
MyClass.class.getResourceAsStream("myFile.txt").
JB.
Lew - 15 Oct 2007 16:08 GMT
> And, if you read the remaining of the documentation, you'll find this:
>
[quoted text clipped - 12 lines]
> the class, you mya just use
> MyClass.class.getResourceAsStream("myFile.txt").
Wouldn't getClass().getClassLoader().getResourceAsStream("myFile.txt") return
the same resource?

Signature
Lew
Jean-Baptiste Nizet - 15 Oct 2007 16:13 GMT
> > This means that if the file is stored in the same package/directory as
> > the class, you mya just use
> > MyClass.class.getResourceAsStream("myFile.txt").
>
> Wouldn't getClass().getClassLoader().getResourceAsStream("myFile.txt") return
> the same resource?
No. It would return null. The above code would only load myFile.txt if
myFile.txt is at the root (in the default package).
JB.
> --
> Lew
Lew - 15 Oct 2007 16:15 GMT
>>> This means that if the file is stored in the same package/directory as
>>> the class, you mya just use
[quoted text clipped - 4 lines]
> No. It would return null. The above code would only load myFile.txt if
> myFile.txt is at the root (in the default package).
Thank you. I have new understanding.

Signature
Lew
Mark Space - 15 Oct 2007 18:00 GMT
> Classloader.getResourceAsStream() or Class.getResourceAsStream() both
> return an InputStream which you can use to read the file.
>
> The first one allows you to specify the location inside the jar file,
> the second one assumes the "resource" file is located in the same
> package (i.e. directory) where the .class file is located.
I had to read this whole thread like four times before I finally got
exactly what was being said here. That *is* an evil collision of method
names here. Seems to me that Sun should deprecate the first and provide
a non-deprecated version named Classloader.getResourcePathAsStream() or
something...
>I would like to extract a file included in the jar file of my application. I
>would like to get it as a byte[]. Can you tell me how to do that? Also could
>you give me a path to the classes and methods dealing with resources in jar
>files?
a Jar is also a Zip file. See http://mindprod.com/jgloss/zip.html
for simple unpacking.
a

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
> Hello all,
>
[quoted text clipped - 4 lines]
> you give me a path to the classes and methods dealing with resources in jar
> files?
To summarize the two above threads, use getResource() and friends if you
are loading a file from your own currently executing jar:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html
If you are manipulating a separate jar file (perhaps adding a special
config file, or modifying the manifest file) use java.util.jar
http://java.sun.com/j2se/1.4.2/docs/api/java/util/jar/package-summary.html