title: How to load program resources in Clojure
body: <p>How do you load program resources such as icons, strings, graphical elements, scripts, and so on in a Clojure program? I am using a project layout similar to that in many Java projects where there is a "resources" directory hanging off of a "source" directory. A jar file is created from the source and includes the resources, but I can't seem to get the resources loaded as I would in Java.</p>
  
  <p>The first thing I tried was something like</p>
  
  <pre><code>(ClassLoader/getSystemResource "resources/myscript.js")
  </code></pre>
  
  <p>But could never find the resource.</p>
  
  <p>You can do something similar with</p>
  
  <pre><code>...
    (let [cls (.getClass net.mydomain.somenamespace)
          strm (.getResourceAsStream cls name)        ]
  ...
  </code></pre>
  
  <p>where name is the <code>name</code> of the resource to load, but the stream is <code>nil</code>.</p>
  
  <p>You can try using the context class loader with something like</p>
  
  <pre><code>...
  
  (let [thr (Thread/currentThread)
        ldr (.getContextClassLoader thr)
        strem (.getResourceAsStream ldr name)]
  ...
  </code></pre>
  
  <p>But <code>strem</code> is always nil.</p>
  
  <p>In frustration, I've tried placing the resource files in just about every directory in the program. They get copied into the jar correctly, but I still can't seem to load them.</p>
  
  <p>I've looked at the language sources for the <code>load</code> function and the run-time library, but am not "getting" it.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p><strong>EDIT</strong>: Here's a more concrete example. In Java, if you wanted to convert MarkDown to HTML, you might use the <code>showdown.js</code> script and write something like:</p>
  
  <pre><code>package scriptingtest;
  
  import java.io.InputStreamReader;
  import javax.script.Invocable;
  import javax.script.ScriptEngine;
  import javax.script.ScriptEngineManager;
  
  public class Example {
  
      private Object converter;
  
      public String transformMarkDown(String markdownString) {
          ScriptEngineManager manager = new ScriptEngineManager();
          ScriptEngine engine = manager.getEngineByName("js");
          try {
              engine.eval(new InputStreamReader(getClass().getResourceAsStream(
                      "resources/showdown.js")));
              converter = engine.eval("new Showdown.converter()");
          } catch (Exception e) {
              return "Failed to create converter";
          }
          try {
              return ((Invocable) engine).invokeMethod(converter, "makeHtml",
                      markdownString).toString();
          } catch (Exception e) {
              return "Conversion failed";
          }
      }
  
      public static void main(String[] args) {
          System.out.println(new Example().transformMarkDown("plain, *emphasis*, **strong**"));
      }
  }
  </code></pre>
  
  <p>when I create the project, it all gets compiled and packed into a jar. When run, the program outputs <code>&lt;p&gt;plain, &lt;em&gt;emphasis&lt;/em&gt;, &lt;strong&gt;strong&lt;/strong&gt;&lt;/p&gt;</code></p>
  
  <p>A literal translation to Clojure seems pretty straightforward, but I run into trouble trying to create the <code>InputStreamReader</code> -- I can't seem to write the code needed to find the script file in the jar.</p>
  
  <p><strong>Edit</strong>: Added "markdown" tag since the post gives two complete examples of approaches to processing markdown.</p>