• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

External jars and clojure

 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....... ?
 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic