views:

79

answers:

2

I'm writing a small app with the help of Eclipse while learning Swing GUIs in Java. I'm also taking my first baby steps in Ant build scripting and have come across a small problem.

I'm parsing an XML file to create objects to store in a List. I've assigned the Ant build script to include the XML file in the Jar package by adding this to the Jar command:

<zipfileset dir="${basedir}" includes="countries.xml" />

To read it I'm doing it through an InputStream:

InputStream is = getClass().getClassLoader().getResourceAsStream(COUNTRY_XML);

and then parse it with the parse()-method of a DocumentBuilder.

To the core of my question, then. After I've run the Ant build script and execute the Jar package, the XML file is easily accessible by the classes and the program runs without problems, but if I only want to test run it in Eclipse (Ctrl+F11) the XML file isn't found, obviously.

Is there a way to remedy this?

A: 

You can add a directory where this file resides to your classpath in Eclipse run configuration

Dmitry
+1  A: 

If you place your XML file in the same directory as your source files in your eclipse project it will be able to find the file when running in eclipse.

If you don't want to add the XML file to your sources you can create a second "source folder" in eclipse and store the file there. At runtime the file should be found.

Bryan Kyle
Wow, it was really that simple. I should have known to put it in the sources directory from the beginning. Worked wonders, just had to change the zipfileset dir attribute to my sources directory.
Patrick