Tasks and classloader

50 views
Skip to first unread message

Julien R.

unread,
Apr 23, 2015, 3:58:39 PM4/23/15
to sbt...@googlegroups.com
Hi,

I’m facing a classpath issue when I try to run a task in my project. My task calls some code that reflectively creates an instance of a class. If I manually create the instance, everything works fine, but when I call this code that uses reflection it fails with a ClassNotFoundException error. My problem is very similar to this one, that was posted more than three years ago:

I was able to resolve my problem by manually setting the classloader using Thread.currentThread.setContextClassLoader, but I’m wondering what should be the recommended way to deal with this situation?

Best,
Julien

Joshua Suereth

unread,
Apr 27, 2015, 8:45:22 AM4/27/15
to sbt-dev
Unfortunately, this is a common problem with classloaders/reflection in Java.   sbt core tasks which load user code (like run, test) will set teh thread context classloader for you.  For now, manually setting in your task is something you have to do.  Just make sure to restore the original context classloader (or null) when you're done.
 
Best,
Julien

--
You received this message because you are subscribed to the Google Groups "sbt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sbt-dev+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sbt-dev/5a2235dd-f992-4e7b-a859-d6069283cbd3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages