Jar

O

Ouabaine

Hello all,

I know this can be done, I know it is simple, but I cant find the methods.

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?

Thanks in advance
 
T

Thomas Kellerer

Ouabaine, 15.10.2007 16:32:
Hello all,

I know this can be done, I know it is simple, but I cant find the methods.

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?

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
 
L

Lew

Thomas said:
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.
 
J

Jean-Baptiste Nizet

Not true. The two are exactly equivalent, in fact, the Class version invokes
the ClassLoader version.

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.
 
R

Roedy Green

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
 
L

Lew

Jean-Baptiste Nizet said:
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").

Wouldn't getClass().getClassLoader().getResourceAsStream("myFile.txt") return
the same resource?
 
J

Jean-Baptiste Nizet

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.
 
L

Lew

Jean-Baptiste Nizet said:
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.
 
M

Mark Space

Ouabaine said:
Hello all,

I know this can be done, I know it is simple, but I cant find the methods.

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?

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
 
M

Mark Space

Thomas said:
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...
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top