Aladdin - Scala Bugtracking
[#1218] project: distribution priority: low category: bug
submitter assigned to status date submitted
Nikolay Lex fixed 2007-07-17 10:27:52.0
subject [contrib #693] NoClassDefFoundError when running script
code
// Tested using 2.5.1-final
// Note that the particular class not found varied as I simplified the example to the following:


// NoClassDefFoundTest8.scala
object Test
{
    abstract class Abstract
    {
        private val content = new scala.collection.mutable.HashMap[Int, String] with scala.collection.mutable.SynchronizedMap[Int, String]

        final def apply(): Nothing =
        {
            throw new ArithmeticException()
        }
    }

    class Concrete extends Abstract
    {
    }

    class MyHolder
    {
        val data = new Concrete()
    }

    class MyThread extends Thread
    {
        override def run(): Unit =
        {
            new MyHolder().data()
        }
    }

    def main(args: Array[String]): Unit =
    {
        new MyThread().start()
    }
}

Test.main(new Array[String](0))

/*

$ scala -howtorun:script NoClassDefFoundTest8.scala

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: $scalascript/Main$Test$1$Abstract$$anon$0
	at $scalascript.Main$Test$1$MyHolder.<init>((virtual file):23)
	at $scalascript.Main$Test$1$MyThread.run((virtual file):30)



To see the expected behaviour:-

Comment out the last line
// Test.main(new Array[String](0))

$ scalac  NoClassDefFoundTest8.scala

$ scala  Test
Exception in thread "Thread-0" java.lang.ArithmeticException
        at Test$Abstract.apply(NoClassDefFoundTest8.scala:9)
        at Test$MyThread.run(NoClassDefFoundTest8.scala:26)





We can achieve the expected behaviour from a script by instantiating a MyHolder before starting a new thread:-

    def main(args: Array[String]): Unit =
    {
        new MyHolder() // added
        new MyThread().start()
    }

$ scala -howtorun:script NoClassDefFoundTest8.scala
Exception in thread "Thread-0" java.lang.ArithmeticException
        at $scalascript.Main$Test$1$Abstract.apply((virtual file):16)
        at $scalascript.Main$Test$1$MyThread.run((virtual file):33)

*/
what happened
Exception in thread "Thread-0" java.lang.NoClassDefFoundError
what expected Exception in thread "Thread-0" java.lang.ArithmeticException
[back to overview]
Changes of this bug report
Nikolay  edited on  2007-07-17 10:28:10.0
Lex  edited on  2007-07-24 17:22:44.0
The problem is that this script creates a separate thread from the main thread. The separate thread continues running after the main thread finishes and the ScriptRunner deletes the compiled class files! I have changed it now to delete the files in a JVM exit hook. A better solution would be to compile and run from in-memory byte arrays, thus avoiding this cleanup entirely....