Skip to content

script with java.sql.Date reference hits a NoClassDefFoundError when running from compiled jar #13760

Closed
@philwalk

Description

@philwalk

Compiler version

Scala code runner version 3.1.1-RC2 -- Copyright 2002-2021, LAMP/EPFL

The problem occurs when running a script with these runtime java versions:

  • openjdk version "16.0.1" 2021-04-20
  • java version "11.0.12" 2021-07-20 LTS

The problem does NOT occur with java 8

  • openjdk version "1.8.0_312"

The symptoms are similar to those reported in #11646, although the problem here is restricted to running the compiled jar.

Minimized code

If a script references java.sql.Date, a NoClassDefFoundError occurs, but only when running from a compiled script jar.

Here's the test script (notice the -save option in the hash bang line):

#!/opt/scala3/bin/scala -save
def main(args: Array[String]): Unit = {
    val d: java.sql.Date = new java.sql.Date(100L) // Compile and execution is successfully.
    println(d)
}

Output

The compile-and-run pass is successful:

/opt/scala3/bin/scala -save sqlDateError.sc
C:/opt/scala3

But because -save is specified, subsequent script runs execute from the compiled jar file, producing the error java.lang.NoClassDefFoundError: java/sql/Date

$ /opt/scala3/bin/scala -save ./sqlDateBug.sc
Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/Date
        at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
        at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166)
        at java.base/java.lang.Class.getMethodsRecursive(Class.java:3307)
        at java.base/java.lang.Class.getMethod0(Class.java:3293)
        at java.base/java.lang.Class.getMethod(Class.java:2106)
        at dotty.tools.runner.RichClassLoader$.run$extension(ScalaClassLoader.scala:91)
        at dotty.tools.runner.CommonRunner.run(ObjectRunner.scala:23)
        at dotty.tools.runner.CommonRunner.run$(ObjectRunner.scala:13)
        at dotty.tools.runner.ObjectRunner$.run(ObjectRunner.scala:48)
        at dotty.tools.runner.CommonRunner.runAndCatch(ObjectRunner.scala:30)
        at dotty.tools.runner.CommonRunner.runAndCatch$(ObjectRunner.scala:13)
        at dotty.tools.runner.ObjectRunner$.runAndCatch(ObjectRunner.scala:48)
        at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:218)
        at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:238)
        at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.ClassNotFoundException: java.sql.Date
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 15 more
``` 

However, there seems to be nothing wrong with the compiled jar itself, which can be run directly by java:

java -jar sqlDateBug.jar

Expectation

A script running from the compiled jar file should behave the same as when doing a compile-and-run.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions