Search code examples
javatomcatservletsrealpath

ServletContext#getRealPath(File.separator) giveing null in tomcat


I use following code in HttpServlet.

String pathToWeb = getServletContext().getRealPath(File.separator);

I have application where above code is working fine and return the real path of application directory. Whereas I send another branch for QA and not able to detect the real-path using above code.

which is always return null, using java8,tomcat8 on windows server. Please assist.

I already gone through question like this but not able to find out the solution.


Solution

  • Use / for the path of the context root:

    getServletContext().getRealPath("/")
    

    instead of the OS-specific file separator.