Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Accessing Files in a JAR

Thread view: 
Bryan - 27 Jul 2007 16:31 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.