6

I am attempting to run a Java web project under Tomcat 8 that traditionally runs under WebSphere. One servlet makes the following call:

xslFilePath = config.getServletContext().getRealPath(System.getProperty("file.separator") + "xsl");

config is an instance of ServletConfig.

The xsl is inside the project and deployed as C:\myproject\build\web\xsl. When the servlet attempts to reference a file located in xslFilePath, I get an exception which indicates that Tomcat is actually looking for the xsl file in C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.3\bin\null. Obviously this is the wrong location, and nothing is found.

Unfortunately I can't change the code because I don't have access to the source. So I would like to know if this is the expected behavior for Tomcat? Is there any Tomcat configuration that will let me ensure the path is referenced to the deployment directory and not to the Tomcat bin directory? Would choosing some other servlet container be preferable? Any advice would be appreciated.

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
MichaelB
  • 571
  • 4
  • 13

1 Answers1

10

Use getRealPath("/xsl").

The parameter for getRealPath() is a 'virtual path' which - unfortunately - is a concept used in the Java docs but not actually defined anywhere. It is assumed to be a path to a resource in your web application and the separator in that case is always '/' regardless of platform.

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
  • 12
    This answer is correct, but it's not just about using the wrong path separator. Prior versions of Tomcat allowed getRealPath("xsl") with no initial separator at all, but Tomcat 8 requires "/xsl" (otherwise returns null). This has broken several of my personal web applications, most of which survived unrecompiled since the days of Tomcat 5... – sam Oct 18 '14 at 15:30