How Do I Tell Clojure Which Classloader to Use?

1,771 views
Skip to first unread message

Nick Mudge

unread,
Jul 4, 2010, 6:53:32 AM7/4/10
to Clojure
I am writing a 3rd party module in Clojure for a Java Web Start
application written in Java.

I am using Clojure 1.1.

I think that my clojure program and clojure.jar are on the classpath
and are being loaded because when clojure.jar is not seen on the class
path this error results: java.lang.ClassNotFoundException:
clojure.lang.IFn
I am not getting that error so I think that my clojure program and
clojure.jar are seen on the classpath and are being loaded.

This is the error I am getting:
Could not locate clojure/core__init.class or clojure/core.clj on
classpath

I think that clojure uses the system classloader by default. Is this
correct? However, according to the Java Web Start documentation, the
system classloader doesn't work with Java Web Start:
http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/faq.html#211

The Java Web Start documentation says that the following classloader
should be used:
ClassLoader cl = Thread.getCurrent().getContextClassLoader();

My question is, how do I tell clojure to use this classloader so that
clojure can load it's own code?

Chas Emerick

unread,
Jul 5, 2010, 7:53:51 AM7/5/10
to clo...@googlegroups.com
Clojure does use the context classloader by default for its "root"
classloader (on top of which it sometimes creates new
DynamicCLassloaders). This behaviour is controlled by the value of
*use-context-classloader*, which is true by default.

I don't have any java web start experience, so I'm afraid I can't
provide any further advice. Anyone else here that does?

- Chas

> --
> 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

Nick Mudge

unread,
Jul 4, 2010, 1:56:38 PM7/4/10
to Clojure
Also I should note that all my clojure code is compiled to classes and
the classes are jarred and sent to the Java Web Start program along
with clojure.jar.


On Jul 4, 3:53 am, Nick Mudge <mud...@gmail.com> wrote:
> I am writing a 3rd party module in Clojure for a Java Web Start
> application written in Java.
>
> I am using Clojure 1.1.
>
> I think that my clojure program and clojure.jar are on the classpath
> and are being loaded because when clojure.jar is not seen on the class
> path this error results: java.lang.ClassNotFoundException:
> clojure.lang.IFn
> I am not getting that error so I think that my clojure program and
> clojure.jar are seen on the classpath and are being loaded.
>
> This is the error I am getting:
> Could not locate clojure/core__init.class or clojure/core.clj on
> classpath
>
> I think that clojure uses the system classloader by default. Is this
> correct? However, according to the Java Web Start documentation, the
> system classloader doesn't work with Java Web Start:http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/faq....

Nick Mudge

unread,
Jul 5, 2010, 12:07:02 PM7/5/10
to Clojure
Is there a way to tell which classloader clojure is using?


On Jul 5, 4:53 am, Chas Emerick <cemer...@snowtide.com> wrote:
> Clojure does use the context classloader by default for its "root"  
> classloader (on top of which it sometimes creates new  
> DynamicCLassloaders).  This behaviour is controlled by the value of  
> *use-context-classloader*, which is true by default.
>
> I don't have any java web start experience, so I'm afraid I can't  
> provide any further advice.  Anyone else here that does?
>
> - Chas
>
> On Jul 4, 2010, at 6:53 AM, Nick Mudge wrote:
>
> > I am writing a 3rd party module in Clojure for a Java Web Start
> > application written in Java.
>
> > I am using Clojure 1.1.
>
> > I think that my clojure program and clojure.jar are on the classpath
> > and are being loaded because when clojure.jar is not seen on the class
> > path this error results: java.lang.ClassNotFoundException:
> > clojure.lang.IFn
> > I am not getting that error so I think that my clojure program and
> > clojure.jar are seen on the classpath and are being loaded.
>
> > This is the error I am getting:
> > Could not locate clojure/core__init.class or clojure/core.clj on
> > classpath
>
> > I think that clojure uses the system classloader by default. Is this
> > correct? However, according to the Java Web Start documentation, the
> > system classloader doesn't work with Java Web Start:
> >http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/faq....

Nick Mudge

unread,
Jul 5, 2010, 9:20:03 PM7/5/10
to Clojure
I found the answer and fixed it and it works!

Yes, *use-context-classloader* is true by default.

But, the context classloader wasn't the right classloader, so I had to
set context classloader to the right classloader, at the right place
before any clojure code is called like this:
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

Whoop!

Found this online which also helped me figure this out:
http://www.assembla.com/spaces/clojure/tickets/260-cannot-load-clojure-classes-from-jar-files-outside-classpath-using-urlclassloader


Mudge



On Jul 5, 4:53 am, Chas Emerick <cemer...@snowtide.com> wrote:
> Clojure does use the context classloader by default for its "root"  
> classloader (on top of which it sometimes creates new  
> DynamicCLassloaders).  This behaviour is controlled by the value of  
> *use-context-classloader*, which is true by default.
>
> I don't have any java web start experience, so I'm afraid I can't  
> provide any further advice.  Anyone else here that does?
>
> - Chas
>
> On Jul 4, 2010, at 6:53 AM, Nick Mudge wrote:
>
> > I am writing a 3rd party module in Clojure for a Java Web Start
> > application written in Java.
>
> > I am using Clojure 1.1.
>
> > I think that my clojure program and clojure.jar are on the classpath
> > and are being loaded because when clojure.jar is not seen on the class
> > path this error results: java.lang.ClassNotFoundException:
> > clojure.lang.IFn
> > I am not getting that error so I think that my clojure program and
> > clojure.jar are seen on the classpath and are being loaded.
>
> > This is the error I am getting:
> > Could not locate clojure/core__init.class or clojure/core.clj on
> > classpath
>
> > I think that clojure uses the system classloader by default. Is this
> > correct? However, according to the Java Web Start documentation, the
> > system classloader doesn't work with Java Web Start:
> >http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/faq....
Reply all
Reply to author
Forward
0 new messages