0

I am parsing XML file in scala using

val data = XML.loadFile("changes.xml")

I have the changes.xml under the src folder. When I run this the code throws a FileNotFoundException. Any idea how to solve it or any insight on how scala looks for the files in the classpath would be helpful.

2
  • Are you working with eclipse? As far as I know the default path is the one above src. So use: XML.loadFile("src/changes.xml") Commented Sep 19, 2011 at 7:39
  • your suggestion has helped but the path of the XML is configurable in such case i want to mention the full path of the xml file like "com/x/y/z/config.xml". Commented Sep 20, 2011 at 1:52

2 Answers 2

3

See what the current directory is using

new java.io.File(".").getCanonicalPath()

Since you're opening the file with a relative path, it looks for the file in the process's working directory.

Sign up to request clarification or add additional context in comments.

Comments

2

Since you're placing your file in the source tree, I assume you want to ship it with the application jar.

Then tell your IDE to copy the file to the ouput folder (maybe this is already happening) so it lies in the classpath. If you place your file in the same folder as the class from which you want to load you can simply do the following. Use in Java/Scala

Class.getResourceAsStream("changes.xml")

Link to API doc.

Edit

You can use XML.load("changes.xml") and I think it will load the file in the same way as Class.getRessourceAsStream. So try putting your xml file into the same folder as the class and make sure the build process copies it into your binary output folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.