• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Placing XML on the classpath

 
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I am having a problem dealing with this issue, Don't know whether it is feasible or not.
The situation is , I have a jar that reads a specific xml file from the class path using the following code.



The jar which I have developed will be used in multiple environments like servlets , swing etc.

The restriction I have here is that the "TO_BE_READ.xml" file has to be in a jar or to be placed along with the class that reads it.

My question is , Is it possible to place that xml file separate from the classes.
Like, if I am putting jar on class path of an application, then is it possible for the jar to locate and read the xml file with in the application.


Regards


 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jars don't locate anything. They're just hunks of data. You're also mixing up references to two different Jars: The Jar you're developing and one that contains the XML file. Please be more clear in which you are referring to, when you explain your requirements.

Now, what I understand what you want to do is read a file that's stored in a Jar file, which is on the classpath.

If you know that the file will always be bundled together with a specific (related) class, you can invoke getResourceAsStream() on the class literal (e.g. MyClass.class.getResourceAsStream("/directory/file.xml");).

If the xml file has no related class, then you can use the system class loader to get the resource: ClassLoader.getSystemResourceAsStream().

A third and more troublesome way is to handle the Jar file directly, using the java.util.jar.JarFile class.
 
I brought this back from the farm where they grow the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic