aspose file tools*
The moose likes Clojure and the fly likes External jars and clojure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Languages » Clojure
Bookmark "External jars and clojure " Watch "External jars and clojure " New topic
Author

External jars and clojure

jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hi guys : Im used to letting maven do all my dependency management in java (and lein in clojure). Thats the good news. The bad news is that I have a java app, and wanted to use it as a library in a clojure script.

What is the simple way to add (non Sun) java classes to a clojure application (without explicitly modifying the system classpath, of course) ?

I was wondering wether maybe I could just "jar" up my application and put it in the root directory of my clojure source code....... ?
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

There's a nice plugin for Leiningen that simplifies working with a local Maven repo that will probably do what you want:

https://github.com/kumarshantanu/lein-localrepo
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
I like that idea, but I actually wanted to keep things lightweight (no plugins, etc.) .... Is there a native "load-jar" type of command ?
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

No, you'd have to mvn install your JAR (and then depend on it) if you don't want to want to use a plugin.
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hmmm im thinking this might be a misunderstanding.

So there is no native support in clojure for importing jar files?

Well... I guess there is always runtime.exec

But that seems like overkill... Java supports dynamic class loading, so I would think there would be a "code" based way to load classes from a jar using standard Jdk+clojure without leinigan/maven ...?
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

Perhaps I'm making assumptions about how you're running your Clojure code...

So, how exactly are you running your Clojure code?
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
I was launching "REPL" (still not quite sure why i need to launch a web-server to run a program) through a shell script, but then i got my hands on the eclipse plugin, which launches repl in a strange, somewhat counter-intuitive manner (albeit convenient) . . .

I dont have any one way of running clojure --- i basically want to know what the simplest, dependency free way to launch it will be. I want it to be 100% jar driven, and i dont wany to rely on other constructs....

thanks....!
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Another interesting clojure question regarding forms :

I noticed that

let [[ x y z ] [1 2 3 4 5]]

somehow maps x y and z into 1 2 3....

How does clojure know how to do this ? Is it capable of inferring the similarity of the two data structures, and then intelligently guessing how i want to map them ? I always assumed that each var in a let statement would have to be a variable. For example

let [ x 1
y 2 ]

maps x to 1, and y to 2.

By that logic, let [ [x y z] ] should throw an "exception" (or compile error) since the first character is a "[" and not a var name ?
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 267
    
    5

REPL - Read-Eval-Print-Loop - is not a web server, it's an interactive console, much like Ruby, Groovy and Scala all have. Which shell script were you using to launch it?

If you're launching the REPL in Eclipse, you can just add your JAR to your Build Path in Eclipse and then (import ...) your Java classes.

I strongly recommend Leiningen as your default build tool. It makes dealing with dependencies and packaging your application really simple - much simpler than any manual process you will come up with

Now, on to let:

That's called destructuring. You can destructure vectors and maps in a binding (and therefore in function arguments too).

Here's a more complex example to show you how powerful the matching and binding can be:

This shows map destructuring with a default (for c, because it's not in the map - the first element of the vector) as well as nested destructuring and the use of & to match the "rest" of a sequence - [f & g] is bound to [[5 6] 7] so f is bound to [5 6] and g is bound to the rest of the enclosing sequence: (rest [[5 6] 7]) is (7).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: External jars and clojure