Java ScriptEngine cannot cast to Scripted

Hello,
I have an issue with Scala 2.12.2 where when using ScriptEngine, I cannot cast the ScriptEngine object to be a Scripted object.

java.lang.ClassCastException: scala.tools.nsc.interpreter.Scripted cannot be cast to scala.tools.nsc.interpreter.Scripted

I am performing a simple cast:

Scripted interpreter = (Scripted) engine;

where the engine object is an instance of javax.script.ScriptEngine

I was wondering if anyone knew how to fix this.

Thank you,
Trevor Flynn

That sort of error is usually due to multiple class loaders.

There’s at least one issue running under sbt. ticket

The problem is that the Scripted object that is being casted to is being loaded by: sun.misc.Launcher$AppClassLoader@73d16e93, which is the system default class loader.
The ScriptEngine instance of Scripted was loaded in by: scala.reflect.internal.util.ScalaClassLoader$URLClassLoader@6397248c, scala’s internal class loader.

Making it impossible to get the ScriptEngine instance of Scripted, as far as I can tell.

Normally, this should not be a problem: each class loader should first
delegate to its parent class loader before it loads anything itself, and
any user-provided class loader should have the application class loader as
an ancestor (parent, parent of parent, etc)

I would check who the ancestors of the current class loader are, and
whether the two class loaders actually return different class objects when
asked to resolve the same class.

The bug is this one.

It has a PR since January.