Accessing Files in a JAR

B

Bryan

Hello all,

I'm wondering if it's possible to directly access files in a JAR. For
example, I have a JAR called foo.jar, and inside that JAR is a package
called my.foo.package. Within that package, I have an XML file that I
need to be able to parse. Before sticking the package in a JAR, I
could simply do something like:

parser.parse("src/my/foo/package/bar.xml")

Is there a way to do something similar to this when the XML file is
inside a JAR?

Thanks in advance! -- BTR
 
N

Nigel Wade

Bryan said:
Hello all,

I'm wondering if it's possible to directly access files in a JAR. For
example, I have a JAR called foo.jar, and inside that JAR is a package
called my.foo.package. Within that package, I have an XML file that I
need to be able to parse. Before sticking the package in a JAR, I
could simply do something like:

parser.parse("src/my/foo/package/bar.xml")

Is there a way to do something similar to this when the XML file is
inside a JAR?

Thanks in advance! -- BTR

I can't give a definitive answer, but you might start by looking to see if the
ZipFile class (and associated stream classes) can do what you require. A jar is
just a Zip file.
 
A

Andrew Thompson

Bryan wrote:
..
Is there a way to do something similar to this when the XML file is
inside a JAR?

Abso(frigging)lutley! If you have some code that is *failing*
to find XML (GIF, txt, whatever) files within the jar's on the
*classpath* of the application, give us an SSCCE and we
can provide futrher pointers* as to getting access to (non
class) resources within jar archives.

* There are some quirks to loading resources that regularly
catch people up, but the possibilities are too many to pin
down, without code. An SSCCE** that fails for you, might
help there.

** <http://www.physci.org/codes/sscce.html>

As an aside. An URL for any 'failing' applet can really
help debugging. Is you applet 'live'? What is the URL?

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200707/1
 
T

Thomas Fritsch

Bryan said:
I'm wondering if it's possible to directly access files in a JAR. For
example, I have a JAR called foo.jar, and inside that JAR is a package
called my.foo.package. Within that package, I have an XML file that I
need to be able to parse. Before sticking the package in a JAR, I
could simply do something like:

parser.parse("src/my/foo/package/bar.xml")

Is there a way to do something similar to this when the XML file is
inside a JAR?
Because you have also packages and class files in your jar file, I
assume the jar file is part of your classpath.
Then you can use the classloader to get your XML file:
URL url = getClass().getResource("/my/foo/package/bar.xml");
System.out.println("url = " + url);
if (url == null)
throw ...;
parser.parse(url.toString());

To understand why this should work, there are some API docs to be read:
Class#getResource, URL, Parser#parse
 
B

Bryan

Hi Andrew,

Not sure what you mean by "SSCCE". This is not a live applet, so I
can't give you a URL. Thomas' suggestion worked for me anyway.

Thanks!
 
L

Lew

Thomas said:
Because you have also packages and class files in your jar file, I
assume the jar file is part of your classpath.
Then you can use the classloader to get your XML file:
URL url = getClass().getResource("/my/foo/package/bar.xml");
System.out.println("url = " + url);
if (url == null)
throw ...;
parser.parse(url.toString());

To understand why this should work, there are some API docs to be read:
Class#getResource, URL, Parser#parse

You can also use getResourceAsStream().
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top