title: How to create a command-line executable in Clojure
body: <p>I have written a program in Clojure and I'd like to execute it on the command-line <em>without</em> specifically invoking java on the command-line (such as <code>java -jar</code>).  I want a single executable file, such as <code>myprogram</code>, that accepts any arguments and runs my program.  Here are a couple things that might make this easier:</p>
  
  <ol>
  <li>It's OK to assume Java is installed and that <code>java</code> is in the path.</li>
  <li>Although a solution that works on Windows would be a great plus, you can assume that this is all being done on a UNIX-like operating system such as Mac OS X or Ubuntu.</li>
  <li>It's OK to invoke Java in a script of some kind.</li>
  <li>It's OK to use some other language, such as Ruby, Python or Perl, which the user may or may not have installed.  All-bash would be cool because I can assume people have that.</li>
  <li>It's OK if I have to use some kind of tool to build a binary that will execute, but I am not looking for a .app or .exe file that expects to operate with a GUI interface (so for example, Oracle's appbundler is not what I'm looking for here).</li>
  </ol>
  
  <p>I have gotten pretty far down this path with one approach, but I have to wonder if there's a better way.</p>
  
  <p><strong>FOR REFERENCE ONLY: What I've Tried Already</strong></p>
  
  <p>I'll describe my approach below, but any answers don't need to follow this approach at all.</p>
  
  <p>What I have done is create a lein plugin called <code>makescript</code> that generates an uberjar, base64 encodes it, and places it inside a Ruby script in a so-called heredoc variable, like this:</p>
  
  <pre><code># ...ruby script...
  BASE64_JAR = &lt;&lt;-JAR_BOUNDARY
  # [...base64 encoded file here...]
  JAR_BOUNDARY
  </code></pre>
  
  <p>You should then be able to run the ruby script.  It will take the BASE64_JAR variable, unencode it, place it in a temporary file, and execute it by invoking <code>java -jar &lt;filename&gt;</code>.</p>
  
  <p>The problem I'm having with this approach is that Ruby's <code>base64</code> library and Clojure's <code>clojure.data.codec.base64</code> libraries seem to be producing different strings to represent the jar, and a string encoded by Clojure does not decode to the original file if I use Ruby.  This may have something to do with the encoding of the strings themselves (UTF-8-related? maybe) between the two languages.  Here are repl/irb sessions that illustrate the disconnect:</p>
  
  <pre><code>repl=&gt; (def jar-contents (slurp "../target/myproj-0.1.0-SNAPSHOT-002-standalone.jar"))
  repl=&gt; (count jar-contents) ;; =&gt; 9433328
  repl=&gt; (def a-str (String. (clojure.data.codec.base64/encode (.getBytes jar-contents)) "UTF-8"))
  repl=&gt; (count a-str)        ;; =&gt; 23265576
  
  irb&gt; f = File.open("target/pwgen-0.1.0-SNAPSHOT-002-standalone.jar", "r").read()
  irb&gt; p f.length   # =&gt; 9657639
  irb&gt; b = Base64.encode64(f)
  irb&gt; p b.length   # =&gt; 13564973
  </code></pre>
  
  <p>Note the raw sizes are close but not the same, but the encoded versions are much different.</p>
  
  <p>Although this is puzzling and I'd like to know why this happens, I think I can bypass the problem by having makescript just generate the uberjar and pass the path to another Ruby script, which then will base64 encode (and later decode, also using Ruby) the standalone JAR.  The question still stands: is there a better, simpler way?  Am I missing something obvious, or is this really as hard as it seems?</p>