clojure: ExceptionInInitializerError in Namespace.<init> loading from a non-default classpath

Posted by Charles Duffy on Stack Overflow See other posts from Stack Overflow or by Charles Duffy
Published on 2012-03-21T22:48:44Z Indexed on 2012/03/21 23:30 UTC
Read the original article Hit count: 269

Filed under:
|

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] (.toLower 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

cl = 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, cl)

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.

© Stack Overflow or respective owner

Related posts about clojure

Related posts about classloader