Relative file paths differ between Jetty and Tomcat?

31 views
Skip to first unread message

Simon Brooke

unread,
Apr 14, 2014, 11:14:22 AM4/14/14
to clojure...@googlegroups.com
I've written a little web-app which uses data stored as text files. I created the project as a luminus web project, using

lein new luminus

so it has the default luminus setup. I stored my datafiles in resources/public/txt, and referred to them in my functions using
 
  14 (defn generate-nonsense [request-map]
  15     (let [file (str "resources/public/txt/" (:file request-map))
  16           depth 3
  17           length (read-string (:length request-map))]
  18       (synthesise/write-output
  19         (synthesise/compose-nonsense
  20           (analyse/analyse-file file depth)
  21           length) true)))

That works fine when you run it from

lein ring server-headless

which as I understand it is actually running Jetty; but when I packaged it using

lein ring uberwar

and sent the resulting WAR file up to my (tomcat) server, it failed with file not found errors. So, what would the file path be when running from a WAR file, why is it different from Jetty, and is there any code which will resolve to the same path whether called from Tomcat or Jetty (or, indeed, other Servlet containers?)

James Reeves

unread,
Apr 14, 2014, 4:33:47 PM4/14/14
to clojure...@googlegroups.com
When in your project directory, certain directories, such as src and resources, are added to the classpath. When you turn your project into a war or jar file, these top-level directories disappear, and their contents folded together within the resulting war or jar.

Referring to resources by filepath is therefore very fragile. In your project directory, the filepath might look like:

    resources/public/foo.html

But when compiled into a jar, it would be:

    public/foo.html

And in a war:

    WEB-INF/classes/public/foo.html

That's why your code won't work outside your project repository.

Instead, you should be using the `clojure.java.io/resource` function, which returns a URL pointing to the file. You can slurp this file, or open an input-stream or reader to it using the functions in clojure.java.io.

- James


--
You received this message because you are subscribed to the Google Groups "Clojure Web Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-web-d...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages