1
votes

In attempting to load an AOT-compiled class from a non-default classpath, I receive the following exception:

Traceback (innermost last):
  File "test.jy", line 10, in ?
        at clojure.lang.Namespace.<init>(Namespace.java:34)
        at clojure.lang.Namespace.findOrCreate(Namespace.java:176)
        at clojure.lang.Var.internPrivate(Var.java:149)
        at aot_demo.JavaClass.<clinit>(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
java.lang.ExceptionInInitializerError: java.lang.ExceptionInInitializerError

I'm able to reproduce this with the following trivial project.clj:

(defproject aot-demo "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.3.0"]]
  :aot [aot-demo.core])

...and src/aot_demo/core.clj defined as follows:

(ns aot-demo.core
  (:gen-class
    :name aot_demo.JavaClass
    :methods [#^{:static true} [lower [java.lang.String] java.lang.String]]))

(defn -lower [str] (.toLowerCase str))

The following Jython script is then sufficient to trigger the bug:

#!/usr/bin/jython
import java.lang.Class
import java.net.URLClassLoader
import java.net.URL
import os

customLoader = java.net.URLClassLoader(
    [java.net.URL('file://%s/target/aot-demo-0.1.0-SNAPSHOT-standalone.jar'
                  % (os.getcwd()))])

java.lang.Class.forName('aot_demo.JavaClass', True, customLoader)

However, the exception does not occur if the test script is started with the uberjar already in the CLASSPATH variable.

What's going on here? I'm trying to write a plugin for the BaseX database in Clojure; the above accurately represents how their plugin-loading mechanism works for the purpose of providing a SSCE for this problem.

1
Looks like this is CLJ-260, "Resolution: Declined". dev.clojure.org/jira/browse/CLJ-260Charles Duffy

1 Answers

0
votes

The plugin-loading mechanism used by BaseX should be extended to modify the current thread's context classloader. For the given sample code (in Jython), this would look something like the following:

currentThread = java.lang.Thread.currentThread()
oldLoader = currentThread.getContextClassLoader()

currentThread.setContextClassLoader(customLoader)
try:
    cls = java.lang.Class.forName('aot_demo.JavaClass', True, customLoader)
finally:
    currentThread.setContextClassLoader(oldLoader)