Search code examples
javaeclipsejsfpathrelative-path

Persistence with files in JSF


I'm developing a JSF web application, with the current 2.1.7 JSF version.

Actually, I have a login page, associated with a session bean, which calls to a logic class "PersonCatalog", which is responsible for reading a text file .txt with the information of the persons, like a database but with files.

The problem is to access the file,
ever I get a NullException because the program doesn't finds the file. I'm not sure about the path I choose, here is the directory herarchy:

Application

 |-- build
 |-- src
       |--eci
           |--pdsw2012
               |--shoeople
                   |--beans
                       |--loginBean and others ...
                   |--logic
                       |--PersonCatalog and others...
                   |--persistence
                       |--files
                           |--persons.txt
 |-- WebContent
       |--WEB-INF
       |--META-INF
       |--a lot of xhtml pages ...

I'm using in PersonCatalog for read the person.txt file the follow code:

File readFile = new File(new String("src/eci/pdsw2012/shoeople/persistence/files/persons.txt"));

But I get:

java.lang.NullPointerException
at eci.pdsw2012.shoeople.logic.PersonCatalog.loadFile(PersonCatalog.java:31)

I have used the ExternalContext().getRealPath(), but it only shows inside Web-content folder.

I'm using Eclipse, and the default route is the installation folder of Eclipse, the is so strange:

String ruta = System.getProperty("user.dir");
System.out.println("DirAbs: " + ruta.getAbsolutePath());

and prints

 C:/Eclipse/

Solution

  • The root of a deployed web application in this case would be the WebContent folder, which contains WEB-INF.

    In the deployed version the src files are compiled into class files and placed in the following folder classes.

    The path to persons.txt will be \WEB-INF\classes\eci\pdsw2012\shoeople\persistence\files\persons.txt