Cannot load clojure classes from Jar files outside classpath using URLClassLoader
Key details
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
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