Skip to content

ClassNotFoundException when using java.sql package of JDK11 #11646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kynthus opened this issue Mar 8, 2021 · 2 comments · Fixed by #11658
Closed

ClassNotFoundException when using java.sql package of JDK11 #11646

kynthus opened this issue Mar 8, 2021 · 2 comments · Fixed by #11658
Assignees
Milestone

Comments

@kynthus
Copy link

kynthus commented Mar 8, 2021

Compiler version

3.0.0-RC1 with Java SE Development Kit 11.0.9

Minimized code

val d: java.sql.Date = new java.sql.Date(100L)

Output

# java -version
java version "11.0.9" 2020-10-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.9+7-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.9+7-LTS, mixed mode)

# scala -version
Starting scala3 REPL...
Scala compiler version 3.0.0-RC1 -- Copyright 2002-2021, LAMP/EPFL
scala> val d: java.sql.Date = new java.sql.Date(100L)
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.getDeclaredMethods(Class.java:2309)
        at dotty.tools.repl.Rendering.valueOf(Rendering.scala:82)
        at dotty.tools.repl.Rendering.renderVal(Rendering.scala:121)
        at dotty.tools.repl.ReplDriver.$anonfun$13(ReplDriver.scala:308)
        at scala.collection.immutable.List.flatMap(List.scala:293)
        at scala.collection.immutable.List.flatMap(List.scala:79)
        at dotty.tools.repl.ReplDriver.extractAndFormatMembers$2(ReplDriver.scala:308)
        at dotty.tools.repl.ReplDriver.renderDefinitions$$anonfun$2(ReplDriver.scala:331)
        at scala.Option.map(Option.scala:242)
        at dotty.tools.repl.ReplDriver.renderDefinitions(ReplDriver.scala:334)
        at dotty.tools.repl.ReplDriver.compile$$anonfun$2(ReplDriver.scala:253)
        at scala.util.Either.fold(Either.scala:189)
        at dotty.tools.repl.ReplDriver.compile(ReplDriver.scala:269)
        at dotty.tools.repl.ReplDriver.interpret(ReplDriver.scala:197)
        at dotty.tools.repl.ReplDriver.loop$1(ReplDriver.scala:130)
        at dotty.tools.repl.ReplDriver.runUntilQuit$$anonfun$1(ReplDriver.scala:133)
        at dotty.tools.repl.ReplDriver.withRedirectedOutput(ReplDriver.scala:152)
        at dotty.tools.repl.ReplDriver.runUntilQuit(ReplDriver.scala:133)
        at dotty.tools.repl.Main$.main(Main.scala:6)
        at dotty.tools.repl.Main.main(Main.scala)
Caused by: java.lang.ClassNotFoundException: java.sql.Date
        at dotty.tools.repl.AbstractFileClassLoader.findClass$$anonfun$1(AbstractFileClassLoader.scala:20)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.immutable.List.foreach(List.scala:333)
        at dotty.tools.repl.AbstractFileClassLoader.findClass(AbstractFileClassLoader.scala:22)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        at dotty.tools.repl.AbstractFileClassLoader.loadClass(AbstractFileClassLoader.scala:34)
        ... 22 more

Expectation

You should be able to use the java.sql package without any problems.
There is no problem with JDK8 or Scala 2.13.5.

# java -version
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

# scala -version
Starting scala3 REPL...
Scala compiler version 3.0.0-RC1 -- Copyright 2002-2021, LAMP/EPFL
scala> val d: java.sql.Date = new java.sql.Date(100L)
val d: java.sql.Date = 1970-01-01
# java -version
java version "11.0.9" 2020-10-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.9+7-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.9+7-LTS, mixed mode)

# scala
Welcome to Scala 2.13.5 (Java HotSpot(TM) 64-Bit Server VM, Java 11.0.9).
Type in expressions for evaluation. Or try :help.

scala> val d: java.sql.Date = new java.sql.Date(100L)
val d: java.sql.Date = 1970-01-01

In addition, although it was JDK11 that observed the error, strictly speaking, it seems to occur in JDK9 or later versions.

@odersky
Copy link
Contributor

odersky commented Mar 8, 2021

It would be good to have more info what could be the possible cause if this. Could it be a setup problem?

@kynthus
Copy link
Author

kynthus commented Mar 8, 2021

The OS information and setup procedure are as follows.

OS(VM):CentOS Linux release 7.6.1810 (Core) 3.10.0-957.el7.x86_64
Host OS is Windows10.

# yum install -y jdk-11.0.9_linux-x64_bin.rpm # Download from Oracle Java archive
# tar -xf scala3-3.0.0-RC1.tar.gz             # Download from Dotty releases

After that, setting the PATH to bin dir in Scala 3.0.0-RC1.
And an error when I run the Minimized code in the REPL.

I found out later, it doesn't seem to occur unless it's on the REPL.

// Test.scala

object Test {

  def main(args: Array[String]): Unit = {
    val d: java.sql.Date = new java.sql.Date(100L) // Compile and execution is successfully.
    println(d)
  }

}
# java -version
java version "11.0.9" 2020-10-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.9+7-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.9+7-LTS, mixed mode)
# scalac -version
Scala compiler version 3.0.0-RC1 -- Copyright 2002-2021, LAMP/EPFL
# scalac Test.scala
# scala Test
1969-12-31

# scala
Starting scala3 REPL...
scala> val d: java.sql.Date = new java.sql.Date(100L) // Same as Minimized code...
Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/Date

@smarter smarter assigned smarter and unassigned kynthus Mar 8, 2021
smarter added a commit to smarter/dotty that referenced this issue Mar 8, 2021
On Java 9+, `null` as a parent of a URLClassLoader means the parent is
the bootstrap classloader which doesn't contain modules like `java.sql`,
explicitly use the system classloader instead.

Fixes scala#11646.
smarter added a commit to dotty-staging/dotty that referenced this issue Mar 8, 2021
On Java 9+, `null` as a parent of a URLClassLoader means the parent is
the bootstrap classloader which doesn't contain modules like `java.sql`,
explicitly use the system classloader instead.

Fixes scala#11646.
smarter added a commit to dotty-staging/dotty that referenced this issue Mar 8, 2021
On Java 9+, `null` as a parent of a URLClassLoader means the parent is
the bootstrap classloader which doesn't contain modules like `java.sql`,
explicitly use the platform classloader instead.

Fixes scala#11646.
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants