8

How can I start a Clojure REPL from my java application and provide some "predefined variables" (I´m a Clojure newbie, I guess there´s a better term for that)? In fact I already tried to make it work by coincidence... I started with clojure.main and added an additional call to RT.var(). Is this the correct way to do it?

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. It´s a standalone desktop application that basically displays/edits a pojo data model. I now want to add support to expose the data model from the running application to a clojure repl. I would then be able to modify/inspect the data model thrugh both, the original application and the repl.

zedoo
  • 10,562
  • 12
  • 44
  • 55

1 Answers1

6

you're looking for the clojure.contrib/server-socket's 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 a lot of ideas on this SO question

Community
  • 1
  • 1
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • Not sure if that´s really what I´m looking for. If you say it, then maybe you´re right, but see my edits above. – zedoo Feb 05 '12 at 18:49
  • 1
    then it's even easier than that, just call `create-repl-server 11111` and connect to localhost. you don't have to worry about trying to get a web server to start your repl, you can do it directly. – Arthur Ulfeldt Feb 06 '12 at 17:07
  • But I guess I´d want to start it from inside my java "host" application. – zedoo Feb 11 '12 at 15:55
  • yes, the host needs to run it, see the accepted answer to http://stackoverflow.com/questions/2181774/calling-clojure-from-java – Arthur Ulfeldt Feb 13 '12 at 15:48