views:

1046

answers:

1

I have the following snippet of code:

String path = servletContext.getRealPath("/");

Now I got a bugreport from a user saying that the returned path is not an absolute path. The returned path is 'usr/local/...' instead of '/usr/local/...' , so getRealPath seems to be returning a relative path.

I can see this, because the returned path is logged into a logfile.

My specs are:

  • JBoss 4.0.5.GA
  • Redhat EL 4
  • jdk 1.5.0

See here for the javadoc

+1  A: 

Something like

String path = new File(servletContext.getRealPath("/")).getAbsolutePath();

should solve your problem. (It does not answer your Question though...;-) )

Regards, Jan

Jan
I don't think so :)Say servletContext.getRealPath("/") returns 'usr/local/...'new File("usr/local/...") will ONLY work when the current working directory is / (root).
Jan Hoeve
Did you try it? It will return an absolute file (It does on my mac with jetty running as a servlet container) ;-)
Jan
That goes back to my comment on the question... I wonder if its an implementation issue since you just tested in Jetty and JBoss uses Tomcat I believe. The docs and spec say it should work, but they have to implement it correctly :-)
cjstehno
As far as I remember Jboss uses tomcat by default as it's webcontainer. You can configure it to use Jetty to. Or just try it with "new File("/").getAbsoluteFilePath() as a fallback. ;-)
Jan