Re: import doesn't work when jar was loaded with add-classpath ?

240 views
Skip to first unread message
Message has been deleted

Eric Sessoms

unread,
Dec 13, 2008, 5:21:01 PM12/13/08
to Clojure
Did you ever get this resolved? I just had the same thing start
happening to me today, after not experiencing any problems with it as
recently as yesterday. (What changed? I tried to install swank.
Nuking swank did not fix the problem, tho.) Step by step:

(add-classpath ...) ; seems to work, because
(seq (.getURLs (clojure.lang.RT/baseLoader))) ; shows the added jar or
directory, and
(Class/forName ...) ; returns a result (no exception), but

(import ...) ; fails with ClassNotFoundException and
(ns (:import ...)) ; also fails

I eventually got my code working again by copying the source of the
import function out of core.clj, renaming it "my-import", and changing
all my import statements to "my-import" statements, accordingly.

Anyway, it makes no sense to me. I'm just hoping someone more
experienced will be able to say what's really going on.

On Dec 9, 7:07 am, "rob.blackwell" <rob.blackw...@aws.net> wrote:
> When I load a JAR at runtime with add-classpath
>
> (add-classpath "file:///Users/reb/java/jena.jar")
>
> and try
>
> user=> (import '(com.hp.hpl.jena.rdf.model ModelFactory))
>
> I get
>
> java.lang.ClassNotFoundException:
> com.hp.hpl.jena.rdf.model.ModelFactory (NO_SOURCE_FILE:0)
> user=>
>
> However, I can still use the classes if I fully qualify them.
>
> The same import does work if I add the JAR to the classpath when
> invoking java, but that seems to be less flexible.
>
> Am I doing something wrong?
>
> Is this a bug?
>
> Any advice gratefully received!

bOR_

unread,
Jan 26, 2009, 3:49:34 PM1/26/09
to Clojure
Thanks for that solution. Ran into the same problem as well, and right
now I don't want to solve it, but just finish something else.

On Dec 13 2008, 11:21 pm, Eric Sessoms <nubga...@gmail.com> wrote:
> Did you ever get this resolved?  I just had the same thing start
> happening to me today, after not experiencing any problems with it as
> recently as yesterday.  (What changed?  I tried to install swank.
> Nuking swank did not fix the problem, tho.) Step by step:
>
> (add-classpath ...) ; seems to work, because
> (seq (.getURLs (clojure.lang.RT/baseLoader))) ; shows the added jar or
> directory, and
> (Class/forName ...) ; returns a result (no exception), but
>
> (import...) ; fails with ClassNotFoundException and
> (ns (:import...)) ; also fails
>
> I eventually got my code working again by copying the source of theimportfunction out of core.clj, renaming it "my-import", and changing
> all myimportstatements to "my-import" statements, accordingly.
>
> Anyway, it makes no sense to me.  I'm just hoping someone more
> experienced will be able to say what's really going on.
>
> On Dec 9, 7:07 am, "rob.blackwell" <rob.blackw...@aws.net> wrote:
>
> > When I load a JAR at runtime with add-classpath
>
> > (add-classpath "file:///Users/reb/java/jena.jar")
>
> > and try
>
> > user=> (import'(com.hp.hpl.jena.rdf.model ModelFactory))
>
> > I get
>
> > java.lang.ClassNotFoundException:
> > com.hp.hpl.jena.rdf.model.ModelFactory (NO_SOURCE_FILE:0)
> > user=>
>
> > However, I can still use the classes if I fully qualify them.
>
> > The sameimportdoes work if I add the JAR to the classpath when

gaetan

unread,
Jan 26, 2009, 5:03:51 PM1/26/09
to Clojure
I am not sure of that but it may come, as Eric suggest, from the
import function in core.clj. Indeed this function is based on
Class#forName which use Reflection#getCallerClass to find the class
loader. May be it uses the application class loader instead of the
root class loader of clojure (in which are stored the URL added
dynamically), so it can load resources on the application class path
but not on url class path added later. Eric's trick of using the
source code in another file may have added enough call indirection to
enable Class#forName to use the right class loader. May be a debug
session could help to find the root cause of the problem

Gaetan

Gaetan Morice

unread,
Jan 27, 2009, 2:58:27 AM1/27/09
to Clojure
In fact it has been corrected in the clojure svn.
Sorry for the noise,
Gaetan


2009/1/26 gaetan <gaetan...@gmail.com>
Reply all
Reply to author
Forward
0 new messages