1

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.

Kevin
  • 6,711
  • 16
  • 60
  • 107
  • possible duplicate of [Where to place configuration properties files in a JSP/Servlet web application?](http://stackoverflow.com/questions/2161054/where-to-place-configuration-properties-files-in-a-jsp-servlet-web-application) – Eric J. Jan 14 '13 at 18:51

4 Answers4

2

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:

  1. Straight from the classpath (the src folder, there where your Java classes also are) using ClassLoader#getResourceAsStream():

    Document doc= new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream("appContext.xml"));
    
  2. Straight from the public webcontent (the WebContent folder, there where /WEB-INF folder resides) using ServletContext#getResourceAsStream():

    Document doc= new SAXReader().read(servletContext.getResourceAsStream("/WEB-INF/appContext.xml"));
    

    The ServletContext is in servlets available by the inherited getServletContext() method.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You can embed your config files into your jar/war files

InputStream is = MyClass.class.getResourceAsStream("/com/site/config/config.xml");
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78
0

You could either create a folder with all your configurations and reference it on the class path of the web application when published on the server, or place them under the WebContent folder, in both cases you need to reference them relatively.

Waleed Almadanat
  • 1,027
  • 10
  • 24
0

There can be multiple places where you could place your property files. The selection of the location depends on the architeture of the project. Commonly used location are:

  • /YourProjectRootFolder/src/main/webapp/WEB-INF/properties/XYZ.properties : in the same folder where you have your java class files.
  • YourProjectConfFolderNAme/src/main/resources/XYZ.properties: here all the property files are kept in a seperate location than your project class files.

Both are the same as you need to move all your property files to you server's conf folder.

Abhishek
  • 75
  • 1
  • 4