Creating one jar file that for execution from Java/Clojure

落花浮王杯 提交于 2019-12-01 09:45:34

问题


I'd like to create one jar file to execute that is implemented with java and clojure. This is the step that I took.

Making my java code call clojure code

I could generate a jar file (ThingOne-1.0.0-SNAPSHOT-standalone.jar) that has clojure core and my clojure code, and I also could get a class file (HelloJava.class) that uses the clojure code in the jar file by following this site - http://walkwithoutrhythm.net/blog/2012/03/26/how-to-call-clojure-1-dot-3-functions-from-java/

The java code is as follows: clojure code is imported as ThingOne

import ThingOne.*;

public class HelloJava {
    public static void main(String[] args) {
        System.out.println("Hello from Java!");
        core.foo (12345);
    }
}

I can run this command to use the code:

java -cp 'ThingOne-1.0.0-SNAPSHOT-standalone.jar:.' HelloJava

Making one jar file

I made one jar directory that has this structure.

├── MANIFEST.MF
└── jar
    └── ThingOne-1.0.0-SNAPSHOT-standalone.jar

The content of MANIFEST.MF is

Manifest-Version: 0.1
Main-Class: HelloJava
Class-Path: jar/ThingOne-1.0.0-SNAPSHOT-standalone.jar

I could get one jar file with jar cvfm hello.jar jar/MANIFEST.MF HelloJava.class.

However, the clojure jar file (ThingOne-1.0.0-SNAPSHOT-standalone.jar) is not included in this jar file, but just referenced.

How can I make one jar file that contains the java class file and clojure jar file?


回答1:


If you want to make an all-inclusive jar, use tools like:

  • Jar Jar Links
  • one-JAR™

If you specifically want to package jar files, you need a custom classloader, like:

  • JarClassLoader

That said, I'd consider Dao Wen's suggestion, unless you have a specific need it cannot meet.




回答2:


As far as I understand your use case is as follows: you have a predominately Java application and you want to make use of some Clojure functions from Java. The best way to do it would be to create a :gen-class which will serve as a Facade to your Clojure functionality. For more information please read this post.

You'd need to create a separate Clojure project which will be managed by Leiningen. Result of the project you can publish to your local mavan repository (Leiningen is a facade to Maven).

In Java project add dependency to your Clojure library, you don't have to worry about clojure.jar as Maven will take care of it.

This is a standard way of developing in Java, please don't crate one monolith Jar with Clojure language and your library in it. If you use Leiningen (Maven) you won't need it.

Alternative: I know of another way to structure your project, when Clojure and Java sources are kept together. It's dirtier but it works. If you'd like me to describe it, please let me know.

EDIT: If you configure your project this way it will be always possible to create uberjar of it. Maven has a plugin for that.



来源:https://stackoverflow.com/questions/12341993/creating-one-jar-file-that-for-execution-from-java-clojure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!