1

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

AabinGunz
  • 12,109
  • 54
  • 146
  • 218

3 Answers3

2

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...

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • `getResourceAsStream()` is the way to go. – asgs May 24 '11 at 12:09
  • Thanks this was also a good info, worked for me. Also what if at later point of time i export the project to jar, will it work that time? – AabinGunz May 24 '11 at 12:10
  • @Abhishek Yes, just make sure the resource is there at the root directory. – asgs May 24 '11 at 12:15
  • It should work, since src is a source folder, and the xml file should be copied in the jar. You can check this, if you look into your build output folder (i assume, it is bin). The xml file should be there. – dunni May 24 '11 at 12:19
1

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());
Thor
  • 6,607
  • 13
  • 62
  • 96
  • Ya this works, thanks. What if at later point of time i export the project to jar, will it work that time? Also can't i use `../` here? – AabinGunz May 24 '11 at 12:08
  • I can't rely on `absolute paths` coz i'll be going into a linux box too :) – AabinGunz May 24 '11 at 12:11
  • use getResourceAsStream(resourceName). But without the "src". I recommend to create in Eclipse a folder called "resources" (or something like that). Add this folder to the jar and the path will be the same. If you want to use a File, it will have a relative path to your working directory, if you want to use it later in a resource bundle, the resourceName will be the same. – Thor May 24 '11 at 12:13
1

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/

kensen john
  • 5,439
  • 5
  • 28
  • 36