Improve ClassLoader layering error message

When a class on the project classpath is loaded via reflection by one of
the parents of the project classloader, sbt run can fail with a
mysterious ClassNotFoundException. This is expected behavior but it
wasn't previously explained to the user.

Ref https://github.com/sbt/sbt/issues/5410.
This commit is contained in:
Ethan Atkins 2020-02-01 14:40:05 -08:00
parent 996ee5f0d6
commit 2c473fc31d
1 changed files with 16 additions and 1 deletions

View File

@ -78,7 +78,22 @@ class Run(private[sbt] val newLoader: Seq[File] => ClassLoader, trapExit: Boolea
val main = getMainMethod(mainClass, loader)
invokeMain(loader, main, options)
} catch {
case e: java.lang.reflect.InvocationTargetException => throw e.getCause
case e: java.lang.reflect.InvocationTargetException =>
e.getCause match {
case ex: ClassNotFoundException =>
val className = ex.getMessage
try {
loader.loadClass(className)
val msg =
s"$className is on the project classpath but not visible to the ClassLoader " +
"that attempted to load it.\n" +
"See https://www.scala-sbt.org/1.x/docs/In-Process-Classloaders.html for " +
"further information."
log.error(msg)
} catch { case NonFatal(_) => }
throw ex
case ex => throw ex
}
}
def directExecute(): Try[Unit] =
Try(execute()) recover {