Run Clojure REPL from java, providing custom context

How can I run Clojure REPL from my java application and provide some "predefined variables" (I am new to Clojure, I guess it works better for this)? In fact, I already tried to do this by coincidence ... I started with clojure.main and added an extra call to RT.var (). Is this the right way to do this?

 import clojure.lang.Symbol; import clojure.lang.Var; import clojure.lang.RT; public class MyClojure { final static private Symbol CLOJURE_MAIN = Symbol.intern("clojure.main"); final static private Var REQUIRE = RT.var("clojure.core", "require"); final static private Var MAIN = RT.var("clojure.main", "main"); // Provide some context, this is my custimisation... @SuppressWarnings("unused") final static private Var Test = RT.var("testme", "myvar", "This is the initial value"); public static void main(String[] args) throws Exception { REQUIRE.invoke(CLOJURE_MAIN); MAIN.applyTo(RT.seq(args)); } } 

EDIT: my application is not a web application. This is a standalone desktop application that basically displays / edits the pojo data model. Now I want to add support to map the data model from a running application to Clojure repl. Then I can change / check the data model, both the source application and repl.

+7
source share
1 answer

you are looking for the clojure.contrib / server-socket create-repl-server function :

here is an excerpt from the examples:

 (use '[clojure.contrib.server-socket :only [create-repl-server]]) (gen-class :name "org.project.ReplServer" :implements [javax.servlet.ServletContextListener]) (defn -contextInitialized [this e] (.start (Thread. (partial create-repl-server 11111)))) 

there are also many ideas on this SO

+5
source

All Articles