3

I want to get the absolute path of my app and I'm using the code below:

String pathToSave = FacesContext.getCurrentInstance()
                .getExternalContext().getRealPath("/");

the result is:

/home/ronaldo/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Odontonew/

But I'm waiting for something like: /home/ronaldo/workspace/Odontonew/

What is wrong ?

n00begon
  • 3,503
  • 3
  • 29
  • 42
Ronaldo Lanhellas
  • 2,975
  • 5
  • 46
  • 92
  • 1
    I guess you are running it through your eclipse IDE – Scary Wombat Aug 28 '13 at 04:14
  • That's the path where your web application is deployed. Still, why would you need to access to this path? Are you trying to load some local resource or using it to save your uploaded files? If the latter, it **will** be better uploading the files into a folder external to the application server to begin with. – Luiggi Mendoza Aug 28 '13 at 05:15

1 Answers1

4

ExternalContext#getRealPath() returns the path relative to where the webapp is been deployed, not where the webapp is been developed orso, as you incorrectly seemed to expect.

But, the variable name pathToSave indicates a much bigger problem: you seem to intend to save files in there. This is a bad idea for the reasons mentioned in the following answer: Uploaded image only available after refreshing the page. In a nutshell, save them to a fixed local disk file system path instead. Note that some servers offer ways to make this step easier configurable, e.g. JBoss.

Just stop using getRealPath(). You never need it in real world. In the 10 years I developed Java web applications (it's under the covers coming from ServletContext#getRealPath()), that method was never been useful for anything. Just ignore that method altogether.

See also:

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