Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Placing XML on the classpath

 
Prash Singh
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • 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


 
Stephan van Hulst
Bartender
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic