How do you "boot-strap" clojure from java?

7 views
Skip to first unread message

prhlava

unread,
Apr 25, 2009, 2:10:10 PM4/25/09
to Clojure

Hello,

Currently, I do the following (the clojure application is called
"isi"):

1. set-up a java netbeans project (called isi) with main class
2. add the clojure.jar (and other libraries) to the project
3. in the main class:

package isi;

/**
* loads the clojure script
*/
public class Main {
public static final String[] script = {"@/app/isi.clj"};

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws java.lang.Exception
{
clojure.main.main(script);
}

}

The isi.clj (the clojure application) lives in "java" package called
"app" (under src directory).

When the project is run, the main class loads the isi.clj script. This
also works when project .jar is build and works as expected (and the
isi.clj clojure script gets stored in the source form).

What do you do?

Kind regards,

Vlad

PS: Not that there is a problem (so far) with the above approach, but
I would like to know what other possibilities exist...

ianp

unread,
Apr 25, 2009, 2:35:22 PM4/25/09
to Clojure
I've got a related question: how do you embed Clojure into an existing
application? If I'd like to provide a clojure console in an
application how do I do the following things:

1. Start a REPL - I guess that this can just be done with
clojure.main.main(String...)

2. Redirect STDIN/OUT/ERR so that I can hook the REPL up to, say, a
JTextPane.

3. Set up variables for the console to use?

Ideally I'd like to do something like the following (not real code):

Clojure clojure = new Clojure();
clojure.setIn(myReader);
clojure.setOut(myWriter);
clojure.set("*domain-object-1*", myDomainObject);
clojure.set("*domain-object-2*", myOtherDomainObject);

to let the user interact with the REPL.

Anyone have examples of something like this?

James Reeves

unread,
Apr 25, 2009, 7:10:06 PM4/25/09
to Clojure
Hi Vlad,

Are you aware you can compile Clojure code directly into Java class
files?

- James

prhlava

unread,
Apr 26, 2009, 4:11:43 AM4/26/09
to Clojure


Hello James,

> Are you aware you can compile Clojure code directly into Java class
> files?

Yes, this was my 1st method of distributing clojure apps (in a .jar).
It works.

But what would be really cool, if I could distribute clojure code in
the source form, load it into a java appliation _and_ call the clojure
code (which got compiled on the fly when loaded) from java. But maybe
the nature of
the java language does not permit this.

OTOH, possibly extending the build process in netbeans with
automatically compiling the .clj files to .class files would be
another way...

Kind regards,

Vlad

Christophe Grand

unread,
Apr 26, 2009, 4:33:06 AM4/26/09
to clo...@googlegroups.com
prhlava a écrit :

> But what would be really cool, if I could distribute clojure code in
> the source form, load it into a java appliation _and_ call the clojure
> code (which got compiled on the fly when loaded) from java. But maybe
> the nature of
> the java language does not permit this.
>

clojure.lang.RT.load("my/script.clj") ; your script must be on the classpath
clojure.lang.RT.var("my.ns", "my-var").invoke("hello") ; to call a function

--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)


prhlava

unread,
Apr 26, 2009, 4:44:27 AM4/26/09
to Clojure


Hello Christophe,

Thank you.

> clojure.lang.RT.load("my/script.clj") ; your script must be on the classpath
> clojure.lang.RT.var("my.ns", "my-var").invoke("hello") ; to call a function

This is good (so I should read more then main.clj source code nex
time ;-) ).

Kind regards,

Vlad

ianp

unread,
Apr 27, 2009, 6:01:30 AM4/27/09
to Clojure
Well, I came up with something similar over the weekend, longer notes
about it here:

http://ianp.org/2009/04/embedding-clojure-in-an-existi

Also, since writing thse notes down I've worked out getting my app
data into Clojure; that, combined with Christophe's notes about and
I'm pretty much set to go for most of what I want to do.

Time too look further into the macro system next...

ianp

unread,
Apr 28, 2009, 3:23:46 AM4/28/09
to Clojure
>    http://ianp.org/2009/04/embedding-clojure-in-an-existi
>
> Also, since writing thse notes down I've worked out getting my app
> data into Clojure; that, combined with Christophe's notes about and
> I'm pretty much set to go for most of what I want to do.

Notes and code snippets here

http://ianp.org/2009/04/embedding-clojure-part-2
Reply all
Reply to author
Forward
0 new messages