Search code examples
javaquartz-schedulerrelative-pathcontextpath

Obtaining a context path in a non-servlet class


In my webapp I am using Quartz to call a method defined in some class at some interval, that method as one of the arguments takes a path to a css file in my WebContent directory. My question is how can I obtain the path to that css file from a non-servlet class.

One thing that I did try was I made the class that calls the method extend the HttpServlet so that I can make a call to

String contextPath = getServletContext().getRealPath("");

but that did not work and my application just hangs on that line. I do not want to hardcode the path as this seem to be unprofessional :)


Solution

  • You cannot access the servlet context from the Quartz job as the job is not invoked as a part of the request handling pipeline.

    Why not just make the CSS file path an argument to the job, so that it can be passed by the servlet/web-code scheduling/invoking the Quartz job? See the Quartz documentation for an example.