Help With Java and Reading An XML File

Associate
Joined
30 Dec 2008
Posts
415
Location
York
Hi guys,

I've recently inherited a Java application that needs to be deployed on the web using Java Web Start. As it stands, the program must be run from a directory that has a Data folder along with the .jar file. This is because the application needs to access the resources stored in the data folder.

Now, when I try and turn this in to a Web Start App, it doesn't like having to read from the external folder. As it stands, the code to read in the XML is as follows:

Document doc = docBuilder.parse(new File("data" + File.separator + "configFile1.xml"));

So, I'm trying to add everything that is needed to the Jar file, so that when Web Start executes the Jar file, everything it needs is in there. So, if I move configFile1.xml to the root of my jar file, I assumed I could just alter the code to this:

Document doc = docBuilder.parse(new File("/configFile1.xml"));

And it would look for the file in the root of the Jar, where it is. But when I try this, I get a Exception telling me that the file couldn't be found.

So now I am stuck, and I've bern looking at it on and off for about a week now, it's hurting my head. I'd rather not re-write how it parses the XML files, so I'd like to know if this is do-able how I'm trying, or am I completely off?

A Million Internet Cookies to anyone who can help me...
 
The files aren't relative to the jar file, but relative to your filesystem, which the .jar file just extends. So "/something" is looking at the root of your disk for the file where the .jar resides.

If you've placed the file 3 subdirectories up from where the code is inside the jar/package structure, then use relative pathing from your code to find it like this "../../../configFile1.xml" as that would always be constant wherever the .jar file lived.

Thanks for your help topdog, I understand what you're saying. I've made the changes but I'm still getting an error, so I've probably done something wrong.

The Class that needs the XML file is here:

Root --> Entities --> Factory --> Class.java

And the XML files are in Root. So I changed my code to:

Document doc = docBuilder.parse(new File("../../../configFile1.xml"));

But it still says that the file couldn't be found :(
 
Back
Top Bottom