I have created a Dynamic Web Project in Eclipse and I have a following Java statement that needs to read a config file:
Document doc= new SAXReader().read(new File(ConstantsUtil.realPath+"appContext.xml"));
Basically, ConstantsUtil.realPath will return an empty string.
I tried putting “appContext.xml” under both “src” folder and under “WEB-INF” folder. However, I will always get the following error:
org.dom4j.DocumentException: appContext.xml (The system cannot find the file specified)
I am really confused: in Eclipse, where is the correct place to put my config xml file?
Thanks in advance.
Your concrete problem is caused by using
new File()with a relative path in an environment where you have totally no control over the current working directory of the local disk file system. So, forget it. You need to obtain it by alternate means:Straight from the classpath (the
srcfolder, there where your Java classes also are) usingClassLoader#getResourceAsStream():Straight from the public webcontent (the
WebContentfolder, there where/WEB-INFfolder resides) usingServletContext#getResourceAsStream():The
ServletContextis in servlets available by the inheritedgetServletContext()method.See also: