Clojure

Cannot load clojure classes from Jar files outside classpath using URLClassLoader

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Resolution: Declined
  • Affects Version/s: None
  • Fix Version/s: Release 1.2
  • Component/s: None
  • Labels:
    None

Description

It is not possible to use URLClassLoader to load a class from a jar file if the jar is outside the classpath. It seems that RT.load tries to search for *.clj file within the classpath and doesn't respect the URLClassLoader and then fails with:

Exception in thread "main" java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at Loader.main(Loader.java:17)
Caused by: java.io.FileNotFoundException: Could not locate net/experimentalworks/Foo__init.class or net/experimentalworks/Foo.clj on classpath:
at clojure.lang.RT.load(RT.java:402)
at clojure.lang.RT.load(RT.java:371)
at clojure.core$load_6449$fn_6458.invoke(core.clj:4171)
at clojure.core$load__6449.doInvoke(core.clj:4170)
at clojure.lang.RestFn.invoke(RestFn.java:413)
at clojure.lang.Var.invoke(Var.java:359)
at net.experimentalworks.Foo.<clinit>(Unknown Source)
... 7 more

The attached .tar.gz contains a sample jar file and the clojure file that was used to generate the class files as well as the sample loader. Java oder Scala written classes can be loaded without any problems. For sure the I put the clojure runtime into the classpath but not the loadme-clojure.jar

Activity

Hide
Assembla Importer added a comment -

stu said: Clojure's base loader uses the thread context loader so long as use-context-classloader is true (the default). Make sure context class loader is set, and then set the thread context class loader to be the loader you have created by calling:

Thread.currentThread().setContextClassLoader(loader);

Do this before using the loader to load classes.

Cheers,
Stu

Show
Assembla Importer added a comment - stu said: Clojure's base loader uses the thread context loader so long as use-context-classloader is true (the default). Make sure context class loader is set, and then set the thread context class loader to be the loader you have created by calling:
Thread.currentThread().setContextClassLoader(loader);
Do this before using the loader to load classes. Cheers, Stu

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: