Q: How to get the absolute path of a file in my project (and not of a file stored in a temporary folder) I am using:


*

*Eclipse Java EE IDE for Web Developers version: Mars.2 Release (4.5.2);

*Apache Tomcat v8.0;

*a Web Dynamic project;

*a Java Servlet.


I have a JSON file stored in the ./WebContent folder. I am trying to get the absolute path of the JSON file in this way:
ServletContext sc = request.getSession().getServletContext();


    //String absolutePath = "/Users/kazuhira/Documents/MAC_workspace/lab2_calendario/WebContent/Database/Events.json";
    String relativePath = "eventsBackup.json";
    String filePath = sc.getRealPath(relativePath);

    System.out.println("(saverServlet): the path of the file is "+filePath);
    //System.out.println("(saverServlet): the path of the file is "+absolutePath);

    //File file = new File(absolutePath);
    String content = request.getParameter("jsonEventsArray");

    try (FileOutputStream fop = new FileOutputStream(file)) {
        System.out.println("(saverServlet): trying to access to the file"+filePath);
        // if file doesn't exists, then create it
        if (!file.exists()) {
            System.out.println("(saverServlet): the file doesn't exists");
            file.createNewFile();
            System.out.println("(saverServlet): file "+filePath+" created");
        }

        System.out.println("(saverServlet): writing on the file "+filePath);

        // get the content in bytes
        byte[] contentInBytes = content.getBytes();

        fop.write(contentInBytes);
        fop.flush();
        fop.close();

        System.out.println("(saverServlet): events backup done");

    } catch (IOException e) {
        e.printStackTrace();
    }

and the file path reconstructed from the relativePath is:
/Users/kazuhira/Documents/MAC_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/lab2_calendario/testJson.txt

Why String filePath = sc.getRealPath(relativePath); generates a path on a temporary folder? In which way I can configure the context on the "real" json file (the same I create in the project)?
I suppose Tomcat is working in a temporary context, why? There's a way to tell him to use the same folder of the project?

A: Yes, you can by changing an option in your Server configuration in Eclipse 

Select the second option in the radio button list :)
