Giter VIP home page Giter VIP logo

clojure-executable-jar's Introduction

Clojure Hello World Executable Jar

A toy example to demostrate building an executable jar from a clojure file without a proper build tool. The process only depends on the Java tools java and jar being on the path. Both are included when installing the Java JDK. Tested on OS X.

Versions

Build with

./build.sh

Run with

java -jar build/hello.jar

Overview

To create an executable jar from a clojure source file we need to:

  1. Create a clojure source file with a main method
  2. Compile the file
  3. Merge the compiled class files with those in the clojure .jar into a new .jar file whose manifest's Main-Class points at the class generated for the clojure file

Process

Create a file src/hello/core.clj with (:gen-class) in the ns macro and a (defn -main ...) function

(ns hello.Core
    (:gen-class))

(defn -main [] (println "Hello, World!"))

Create a diretory into which the compiled classes will be placed.

mkdir -p classes

Compile the clojure file using compile. You'll need to add the root directory in which the clojure source file is (src) to the classpath. The directory in which the compiled files will be placed (by default classes) needs to be added as well.

java -classpath lib/clojure-1.8.0.jar:src:classes clojure.main -e "(compile 'hello.Core)"

At this point, the classes directory should contain a hello directory with several .class files in it.

Next, build a jar with the generated classes and those from clojure.jar. For ease, we'll just copy clojure.jar and then modify it.

mkdir -p build
cp lib/clojure-1.8.0.jar build/hello.jar
jar -ufe build/hello.jar hello.Core -C classes/ .

Finally, run the jar. It should print Hello, World!

java -jar build/hello.jar

References

clojure-executable-jar's People

Contributors

cmihalcik avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.