Search code examples
scalasbtclassloader

Custom classloader fails after upgrading sbt from 0.12.2 to 0.13+


After updating my build.properties to use sbt 0.13+ I can no longer dynamically load case classes at runtime.

With sbt 0.12.2 I was able to use a custom classloader, but now with sbt 0.13.2 I get an error: caused by: java.lang.ClassNotFoundException: scala.Product

Here's a runnable example. Running sbt test will show the error.

What changed in sbt 0.13 that puts my dynamic classloading shenanigans to an end, and how might I fix this?

Thanks for taking a look!

-Julian

Edit:

Using sbt 0.13.7-M4 partially solves this problem. sbt run succeeds, while sbt test gives a different error, where it seems that the dynamically loaded class in each test is being loaded twice.


Solution

  • The "Loading twice" error in Test was due to me not putting my setup code within the Specs2 expectation.

    In addition, I need to use a different classloader in sbt 0.13+ : Issue was due to using the wrong classloader.

    First I tried ClassLoader loader = ClassLoader.getSystemClassLoader();, but then that broke.

    Next, with sbt 0.12.2, I tried ClassLoader loader = Thread.currentThread().getContextClassLoader();, but that broke with sbt 0.13+.

    Now I'm using a dummy class to probe and get a classloader a la probe.getClass().getClassLoader(), and that seems to have solved the problem.