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
Nigel Wade - 27 Jul 2007 16:46 GMT
> Hello all,
>
[quoted text clipped - 10 lines]
>
> 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.

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
Andrew Thompson - 27 Jul 2007 16:55 GMT
..
>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?

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Bryan - 27 Jul 2007 20:34 GMT
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!
> .
>
[quoted text clipped - 21 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200707/1
Thomas Fritsch - 30 Jul 2007 11:57 GMT
> Not sure what you mean by "SSCCE".
See here: <http://www.google.de/search?q=SSCCE>

Signature
Thomas
Roedy Green - 30 Jul 2007 17:30 GMT
>Not sure what you mean by "SSCCE".
see http://mindprod.com/jgloss/sscce.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Thomas Fritsch - 27 Jul 2007 17:15 GMT
Bryan schrieb:
> 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
[quoted text clipped - 6 lines]
> 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

Signature
Thomas
Bryan - 27 Jul 2007 20:34 GMT
Thomas,
Your suggestion worked perfectly! Thanks! -- BTR
On Jul 27, 10:15 am, Thomas Fritsch <i.dont.like.s...@invalid.com>
wrote:
> Bryan schrieb:> 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
[quoted text clipped - 21 lines]
> --
> Thomas
Lew - 27 Jul 2007 20:46 GMT
> Bryan schrieb:
>> I'm wondering if it's possible to directly access files in a JAR. For
[quoted text clipped - 18 lines]
> To understand why this should work, there are some API docs to be read:
> Class#getResource, URL, Parser#parse
You can also use getResourceAsStream().

Signature
Lew
Roedy Green - 30 Jul 2007 17:28 GMT
>I'm wondering if it's possible to directly access files in a JAR.
Here are two methods:
1. Use ZipEntry and ZipInputStream, see
http://mindprod.com/jgloss/zip.html
2. use a xxx:jar URL, see http://mindprod.com/jgloss/uri.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com