Question

This is a very simple question for many of you reading this, but it's quite new for me. Here is a screenshot for my eclipse

enter image description here

When i run this program i get java.io.FileNotFoundException: queries.xml (The system cannot find the file specified) i tried ../../../queries.xml but that is also not working. I really don't understand when to use ../ because it means go 1 step back in dir, and in some cases it works, can anyone explain this? Also how can I refer to queries.xml here. Thanks

Note: I might even use this code on a linux box

Was it helpful?

Solution

I assume it is compiling your code into a build or classes folder, and running it from there...

Have you tried the traditional Java way for doing this:

def query = new XmlSlurper().parse( GroovySlurping.class.getResourceAsStream( '/queries.xml' ) )

Assuming the build step is copying the xml into the build folder, I believe that should work

I don't use Eclipse though, so can't be 100% sure...

OTHER TIPS

Try

file = new File("src/org/ars/groovy/queries.xml");

To check the actual working directory of eclipse you can use

File f = new File(".");
System.out.println(f.getAbsolutePath());

You could try using a property file to store the path of the xml files.

This way you can place the xml files in any location, and simply change the property file. This will not require a change/recompilation of code.

This would mean you will only need to hardcode the path of the property file.

If you prefer not hardcoding the path of the property file, then you could pass it as an argument during startup in your server setup file. (in tomcat web.xml). Every server will have an equivalent setup file where you could specify the path of the property file.

Alternatively you could specify the path of the xml in this same file, if you don't want to use property files.

This link will show you an example of reading from property files.
http://www.zparacha.com/how-to-read-properties-file-in-java/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top