Re: Setting different class loader

Mark Evenson <[email protected]> Mon, 6 Feb 2023 10:09:48 +0100
Newsgroups gmane.editors.j.devel
Message-ID <[email protected]>

> On Jul 23, 2020, at 08:37, Steven Nunez <[email protected]> wrote:
>=20
> Thank you Olof; that was just what I needed to get things working. =
Well, that and another half day struggling with what turns out to be a 5 =
year old bug (#388). I really wish someone had mentioned in the JSS =
documentation, "Oh, and this doesn't yet work in top-level forms". All =
told, it was a lot more difficult to get started with ABCL than I =
expected it to be, but I'm glad it's done and grateful to those that =
helped. For reference, here's the code that finally works:
>=20
> (defun change-class-loader ()
>   (#"setContextClassLoader" #1"Thread.currentThread()" =
(java:get-current-classloader)))
> (change-class-loader)
>=20
>=20
> At least it gets me as far as line two of the spark 'hello world'; =
hopefully there aren't any other surprises lurking. If anyone can =
recommend any best practices or improvements, especially around the =
class loader bits, I'd be very happy to hear them.

[=E2=80=A6]
>=20
> Hopefully there's a better way of course, since this is hardly
> convenient.

Slightly more convenient perhaps is to change the context ClassLoader
in the ASDF :PERFORM clause
<https://github.com/easye/ember/blob/master/ember.asd#L7> before
the Spark Maven artifacts are loaded:

    (defsystem ember
      :description "Wrapper for Spark 3.0"
      :defsystem-depends-on (jss abcl-asdf)
      :depends-on (#:jss #:javaparser)
      :perform (load-op (o c)
                        (#"setContextClassLoader" (#"currentThread" =
'Thread)
                                                  =
(java:get-current-classloader))
                        (call-next-method o c))
      :components ((:mvn "org.apache.spark/spark-core_2.12" :version =
"3.0.0")
                   (:file "package")
                   (:file "ember")))

Such a setting of the context ClassLoader seems to be quite useful
for integrating quite a few Java libraries with ABCL, I wonder if
it shouldn't be the default for ABCL-ASDF loading Maven artifacts
to set the context ClassLoader is this manner.  Would such a choice
adversely affect anyones current usage?  I almost exclusively use
ABCL via SLIME, so maybe production use of ABCL (i.e. as a standalone
packaged application) would run into problems here.  Thoughts?

For those using, openjdk17 one needs to set the [following run-time
switch in the JVM][1]:

  =E2=80=94add-opens java.base/sun.nio.ch=3DALL-UNNAMED=20

As a bonus, that annoying [seven (!) year old bug][338], has been
recently been fixed by Alejandro Zamora Fonseca for the [upcoming
abcl-1.9.1 release][1.9.1].
=20

[1]: =
<https://stackoverflow.com/questions/72230174/java-17-solution-for-spark-j=
ava-lang-noclassdeffounderror-could-not-initializ>
[338]: <https://abcl.org/trac/ticket/338>=20
[1.9.1]: <https://github.com/armedbear/abcl/pull/534>

--=20
"A screaming comes across the sky.  It has happened before but there is =
nothing=20
to compare to it now."