简体   繁体   中英

How to Run a jar file which contains clojure and java code compiled class files. java file contains main method

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)); } }
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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM