在java程序中嵌入swank-clojure [英] Embedding swank-clojure in java program
问题描述
import clojure.lang.Compiler;
import java.io.StringReader;
public class Embed {
public static void main(String [] args)throws Exception {
final String startSwankScript =
(ns my-app\\\
+
(:use [swank.swank:as swank]))\\\
+
(swank / start-repl)
Compiler.load(new StringReader(startSwankScript));
}
}
任何帮助非常感激,
hhh
是否可以在Clojure中实现 Embed
?你可以使用 gen-class
(请参阅 Meikel Brandmeyer的教程)和AOT编译。
代码可以像
(ns your-app.Embed
/ pre>
(:require [swank.swank:as swank])
(:gen-class
:method [[startSwank [] void]]))
(defn -startSwank []
(swank / start-repl))
(添加您需要的任何其他内容);然后在应用程序的Java部分中,您可以导入您的Clojure准备的类,实例化它,并在实例上调用
.startSwank()
。
不知道程序化停止Swank ...我很想知道一个很好的方法来做自己。 (如果我想出来,我会回来一个更新;否则,我想阅读别人的答案,详细说明如何去做。)
Based on the Embedding section of http://github.com/technomancy/swank-clojure, I'm using the following to test it out. Is there a better way to do this that doesn't use Compiler? Is there a way to programmatically stop swank? It seems start-repl takes control of the thread. What would be a good way to spawn off another thread for it and be able to kill that thread programatically.
import clojure.lang.Compiler; import java.io.StringReader; public class Embed { public static void main(String[] args) throws Exception { final String startSwankScript = "(ns my-app\n" + " (:use [swank.swank :as swank]))\n" + "(swank/start-repl) "; Compiler.load(new StringReader(startSwankScript)); } }
Any help much appreciated, hhh
解决方案Would it be acceptable to you to implement the
Embed
class in Clojure? You could do that withgen-class
(see Meikel Brandmeyer's tutorial for details) and AOT compilation.The code could go something like
(ns your-app.Embed (:require [swank.swank :as swank]) (:gen-class :methods [[startSwank [] void]])) (defn -startSwank [] (swank/start-repl))
(add anything else you require); then in the Java part of your application, you could import your Clojure-prepared class, instantiate it and call
.startSwank()
on the instance.Not sure about programmatically stopping Swank... I'd be curious to know of a good way to do that myself. (And I'll be back with an update if I figure it out; otherwise, I'd love to read somebody else's answer detailing how to go about that.)
这篇关于在java程序中嵌入swank-clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!