How to access an XML file within a JAR or EAR in Java

There are instances when you would want to access an XML file or a property file bundled along with your jar or ear file. Below code snippet shows you how to access an XML or a property file withing a JAR or EAR. There are two ways of doing it as below:
From Non-Static Context

To access a properties file or an XML file from a non-static context, below code can be used:

ClassAbc is the class file from which you would want to acess the file.
fileName is the name of the file e.g. abc.properties or abc.xml.

From Static Context

To access a properties file or an XML file from a static context, below code can be used:

Please feel free to provide your suggestions for a better way of doing the things. Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">