This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.