0
votes

java -cp clojure.jar clojure.main compile.clj this is compiling the clojure code. javac CalculateSum.java compiling java code. jar cvf sum.jar *.class getting jar file of class files.

java CalculateSum is running main and giving output correctly. How to run jar file from java environment? like java -cp clojure.jar;sum.jar clojure.main CalculateSum where CalculateSum is main class.

sample code _utils.clj_

(ns utils
(:gen-class :name Utils :methods [#^{:static true} [sum [java.util.Collection] long]])) (defn sumx [coll] (reduce + coll)) (defn -sum [coll] (sumx coll))

compile.clj

(set! *compile-path* "./") (compile 'utils)

CalculateSum.java public class CalculateSum { public static void main(String[] args) { java.util.List<Integer> xs = new java.util.ArrayList<Integer>(); xs.add(10);
xs.add(5); System.out.println(Utils.sum(xs)); } }

Aim is to craete jar file out of this code. and run jar file

java should call clojure code, execute it and print result

1
It's not clear whether all the classes are no in jar files, and also whether or not the jar files contain manifests stating the main class, classpath dependencies etc. Some sample code would be useful. - Jon Skeet
@JonSkeet i have edited the question, now you can see the code also. - vikbehal
But no sign of a jar file manaifest. It's also not clear where the compiled clojure code is ending up - are you including that in the jar file too? - Jon Skeet
compiled class files of utils.clj and CalculateSum.java are added to jar file, i don't have anything like manifest. - vikbehal

1 Answers

1
votes

Okay, so there are two things:

  • To run it just from a jar file rather than making it an executable jar file, you should be fine with just:

    java -cp sum.jar CalculateSum
    

    or possibly (if it needs classes from closure.jar at execution time)

    java -cp closure.jar;sum.jar CalculateSum
    
  • To turn it into an executable jar file which you can run with

    java -jar sum.jar
    

    you'll need a manifest file including a Main-Class attribute letting you set the entry point, and possibly a Class-Path attribute to add the closure.jar file to the jar's classpath.

Follow the links for more details.