1

I have a problem with a relative path in my Java EE application, here's is the situation:

  1. I'm trying to parse an xml file that i have in /WebContent/files/queries.xml using SaxParser API.

  2. When I'm providing this path (./files/queries.xml) in a java class so that i can perform the parsing operation, I'm getting a java.io.FileNotFoundException: /home/james/./files/queries.xml !

Could someone please help me with that?

Thanks a lot

2 Answers 2

2

WebContent is the location of your webapp sources. When deployed, everything is bundled into a war file, and can't be accessed using file IO.

Use ServletContext.getResourceAsStream() to get a resource from your webapp's context.

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

2 Comments

Thank's for the answer, actually i'm not using a servlet, but a simple java class ... so how can i do this ?
Your class runs inside a webapp. Pass the servlet context to your class in order to be able to access it. Or store the file in the WEB-INF/classes directory, along with the webapp's classes, and load it using MyClass.class.getResourceAsStream().
1

To get the real path you can just do:

String path = s.getServletContext().getRealPath("/WebContent/files/queries.xml");

2 Comments

Thank's for the answer, actually i'm not using a servlet, but a simple java class ... so how can i do this ?
What type of container are you using?

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.