Adding jar to classpath in REPL

1,258 views
Skip to first unread message

Vitaly Peressada

unread,
Mar 31, 2011, 8:19:11 AM3/31/11
to Clojure
Wanted to add jar to classpath without restarting REPL. Recalled java
reflection hack. Ported to Clojure.

user=> (import (org.apache.commons.cli Parser))
; fails with "#<CompilerException java.lang.ClassNotFoundException:
org.apache.commons.cli.Parser"

user=>
(import (java.io File)
(java.net URL URLClassLoader)
(java.lang.reflect Method))

(defn add-to-cp [#^String jarpath]
(let [#^URL url (.. (File. jarpath) toURI toURL)
url-ldr-cls (. (URLClassLoader. (into-array URL [])) getClass)
arr-cls (into-array Class [(. url getClass)])
arr-obj (into-array Object [url])
#^Method mthd (. url-ldr-cls getDeclaredMethod "addURL" arr-
cls)]
(doto mthd
(.setAccessible true)
(.invoke (ClassLoader/getSystemClassLoader) arr-obj))
(println (format "Added %s to classpath" jarpath))))

; any jar not on a classpath will do
(add-to-cp "c:/m2repo/commons-cli/commons-cli/1.0/commons-
cli-1.0.jar")

; import any class from loaded jar
(import (org.apache.commons.cli Parser))

Has anyone seen something similar? If no, do you think this could be
useful in clojure.contrib.repl-utils or else?

Ambrose B

unread,
Mar 31, 2011, 8:25:32 AM3/31/11
to clo...@googlegroups.com
One alternative to this would be to start a Nailgun server. You can
add to the classpath quite easily.

That said, this looks pretty interesting.

Ambrose

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Laurent PETIT

unread,
Mar 31, 2011, 8:34:47 AM3/31/11
to clo...@googlegroups.com, Vitaly Peressada
There is this which is in Clojure, but is considered deprecated : clojure.core/add-classpath

2011/3/31 Vitaly Peressada <vit...@ufairsoft.com>

isaac praveen

unread,
Mar 31, 2011, 9:16:51 AM3/31/11
to clo...@googlegroups.com, Vitaly Peressada
On Thu, Mar 31, 2011 at 5:49 PM, Vitaly Peressada <vit...@ufairsoft.com> wrote:
> Wanted to add jar to classpath without restarting REPL. Recalled java
> reflection hack. Ported to Clojure.
>
Have a look at http://icylisper.in/jark/classpath.html
Jark is a tool to manage classpaths and clojure namespaces on a persistent JVM.

It gives you a clojure repl as well: jark ns repl NAMESPACE (or simple
jark repl [NAMESPACE])

--
isaac
http://icylisper.in

Vitaly Peressada

unread,
Mar 31, 2011, 9:34:25 AM3/31/11
to Clojure
Thanks Laurent and Isaac for pointers.
It would be interesting to know why clojure.core/add-classpath has
been deprecated in 1.1?

Stuart Sierra

unread,
Apr 2, 2011, 3:45:24 PM4/2/11
to clo...@googlegroups.com
`add-classpath` works only in very limited circumstances. It breaks in most environments that expect to control classloading through other means, like a Java IDE or a servlet container.

-Stuart Sierra
clojure.com
Reply all
Reply to author
Forward
0 new messages