Kotlinc-jvm repl having trouble connecting to mysql database

Hi

I am trying to use kotlinc-jvm to access a mysql database via jdbc. I am running Lubuntu 20.04 on a 64 bit machine. I have found /usr/share/java/mysql-connector-java-8.0.20.jar on my machine.

Having had issues doing this in kotlinc-jvm, I tried the following in jshell:

  • jshell --class-path /usr/share/java/mysql-connector-java-8.0.20.jar

Once inside jshell I try the following (without the leading dash):

  • import java.sql.*;

  • Class.forName(“com.mysql.jdbc.Driver”);

This is successful, though I get a deprecated class warning.

Next I try the following with kotlinc-jvm:

  • kotlinc-jvm -jvm-target 1.8 -cp /usr/share/java/mysql-connector-java-8.0.20.jar

  • import java.sql.*

  • Class.forName(“com.mysql.jdbc.Driver”).newInstance()

and I get a massive list of error messages (displayed below).

I also try

  • Class.forName(“com.mysql.cj.jdbc.Driver”).newInstance()

and get what appears to be the same massive list of error messages.

I also tried jvm target 9 and 11 and had the same issue.

Note that I have gotten this to work in Inteelij but wish to try out the repl in kotlinc-jvm to see if it responds quikcer - I find the repl in Intellij to be very hard to work with and sometimes very slow.

Thanks . . .

Phil

java.lang.NoClassDefFoundError: java/sql/Driver
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:550)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:550)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
Caused by: java.lang.ClassNotFoundException: java.sql.Driver
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:550)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/java.net.URLClassLoader.defineClass(URLClassLoader.java:550)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:458)
at java.base/java.net.URLClassLoader$1.run(URLClassLoader.java:452)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:451)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:576)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
at Line_1.(Unknown Source)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.jetbrains.kotlin.cli.common.repl.GenericReplEvaluator$eval$1$scriptInstance$1.invoke(GenericReplEvaluator.kt:94)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.configuration.SnippetExecutionInterceptor$Plain.execute(SnippetExecutionInterceptor.kt:12)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplInterpreter$eval$evalRes$1.invoke(ReplInterpreter.kt:95)
at org.jetbrains.kotlin.cli.common.repl.GenericReplEvaluator.eval(GenericReplEvaluator.kt:94)
at org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluatorBase.eval(GenericReplCompilingEvaluator.kt:76)
at org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluatorBase.compileAndEval(GenericReplCompilingEvaluator.kt:51)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplInterpreter.eval(ReplInterpreter.kt:90)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal.eval(ReplFromTerminal.kt:103)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal.one(ReplFromTerminal.kt:94)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal.doRun(ReplFromTerminal.kt:59)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal.access$doRun(ReplFromTerminal.kt:26)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal$Companion.run(ReplFromTerminal.kt:159)
at org.jetbrains.kotlin.scripting.compiler.plugin.JvmCliReplShellExtension.run(JvmCliReplShellExtension.kt:24)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:108)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:51)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:85)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:43)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:105)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:83)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:51)
at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMainNoExit(CLITool.kt:219)
at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMain(CLITool.kt:211)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.main(K2JVMCompiler.kt:261)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.main(K2JVMCompiler.kt)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jetbrains.kotlin.preloading.Preloader.run(Preloader.java:87)
at org.jetbrains.kotlin.preloading.Preloader.main(Preloader.java:44)

I think my comment above may be related to this other post 5 years ago - Kotlin Classpath and Java ClassLoaders with REPL - #4 by udalov

Here’s the relevant issue: https://youtrack.jetbrains.com/issue/KT-21140.

As a workaround, you can use JDK 8 to run the Kotlin REPL. For that, you need to ensure it is installed on your system, and then run kotlinc by setting the JAVA_HOME variable (using AdoptOpenJDK 8 as example):

JAVA_HOME=/usr/lib/jvm/adoptopenjdk-8-hotspot-amd64 kotlinc ...

or by passing the -jdk-home argument:

kotlinc -jdk-home=/usr/lib/jvm/adoptopenjdk-8-hotspot-amd64 ...

(Note that the -jvm-target argument only controls what instructions the compiler can use in the generated bytecode; it has no effect on what JDK the compiler uses to resolve symbols in your code.)

Hi

First, thanks for getting back to me. In this reply I will post the results of what you suggested - in my next reply I will post some other results.

As you suggested, I installed adoptopenjdk-8 . . .

I then started up kotlinc and subsequently issued a set of commands (all of which follow):

phil@TroyWareJ5005A:~$ kotlinc-jvm -jdk-home /usr/lib/jvm/adoptopenjdk-8-hotspot-amd64 -classpath /home/phil/TestKotlinC/mysql-connector-java-5.1.49

import java.sql.Connection

import java.sql.DriverManager

import java.util.*

import java.io.*

import java.net.URLClassLoader

internal var connection: Connection? = null

internal var username = “pacuser” // provide the username

internal var password = “yortlihp” // provide the corresponding password

val connectionProperties = Properties()

connectionProperties.put(“user”, username)

connectionProperties.put(“password”, password)

Class.forName(“com.mysql.jdbc.Driver”).newInstance()

I got the following error message:

java.lang.NoClassDefFoundError: java/sql/Driver

Hi

I also tried something different that worked a little but not enough:

I started up kotlinc by entering
kotlinc-jvm -jdk-home /usr/lib/jvm/adoptopenjdk-8-hotspot-amd64 -classpath /home/phil/TestKotlinC/mysql-connector-java-5.1.49

I then entered the following:

import java.sql.Connection
import java.sql.DriverManager
import java.util.*
import java.io.*
import java.net.URLClassLoader
var file = File(“/home/phil/TestKotlinC/mysql-connector-java-5.1.49”)
var classUrl = file.toURI().toURL()
var urls = arrayOf(classUrl)
var ucl = URLClassLoader(urls)
ucl.loadClass(“com.mysql.jdbc.Driver”)

kotlinc responded to the last command with
res9: java.lang.Class<*>! = class com.mysql.jdbc.Driver
(which I interpret to mean that was abled to load the Driver from the jar file - am I correct about that?)

I then entered:

internal var connection: Connection? = null
internal var username = “pacuser” // provide the username
internal var password = “yortlihp” // provide the corresponding password
val connectionProperties = Properties()
connectionProperties.put(“user”, username)
connectionProperties.put(“password”, password)
connection = DriverManager.getConnection(“jdbc:” + “mysql” + “://” + “127.0.0.1” + “:” + “3306” + “/” + “pac”, connectionProperties)

and got the following error messages:
java.lang.NoClassDefFoundError: java/sql/DriverManager
Caused by: java.lang.ClassNotFoundException: java.sql.DriverManager
at java.base/java.lang.ClassLoader.findClass(ClassLoader.java:719)
at org.jetbrains.kotlin.cli.common.repl.ReplClassLoader.findClass(ReplClassLoader.java:44)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at Line_21.(Unknown Source)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.jetbrains.kotlin.cli.common.repl.GenericReplEvaluator$eval$1$scriptInstance$1.invoke(GenericReplEvaluator.kt:94)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.configuration.SnippetExecutionInterceptor$Plain.execute(SnippetExecutionInterceptor.kt:12)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplInterpreter$eval$evalRes$1.invoke(ReplInterpreter.kt:95)
at org.jetbrains.kotlin.cli.common.repl.GenericReplEvaluator.eval(GenericReplEvaluator.kt:94)
at org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluatorBase.eval(GenericReplCompilingEvaluator.kt:76)
at org.jetbrains.kotlin.cli.common.repl.GenericReplCompilingEvaluatorBase.compileAndEval(GenericReplCompilingEvaluator.kt:51)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplInterpreter.eval(ReplInterpreter.kt:90)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal.eval(ReplFromTerminal.kt:103)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal.one(ReplFromTerminal.kt:94)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal.doRun(ReplFromTerminal.kt:59)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal.access$doRun(ReplFromTerminal.kt:26)
at org.jetbrains.kotlin.scripting.compiler.plugin.repl.ReplFromTerminal$Companion.run(ReplFromTerminal.kt:159)
at org.jetbrains.kotlin.scripting.compiler.plugin.JvmCliReplShellExtension.run(JvmCliReplShellExtension.kt:24)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:108)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:51)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:85)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:43)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:105)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:83)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:51)
at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMainNoExit(CLITool.kt:219)
at org.jetbrains.kotlin.cli.common.CLITool$Companion.doMain(CLITool.kt:211)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler$Companion.main(K2JVMCompiler.kt:261)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.main(K2JVMCompiler.kt)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jetbrains.kotlin.preloading.Preloader.run(Preloader.java:87)
at org.jetbrains.kotlin.preloading.Preloader.main(Preloader.java:44)

I do not understand why kotlinc-jvm is not finding DriverManager - am I not referencing the JDK properly? Or is kotlinc still using my other JDK?

Thanks . . .

Phil

Oh, I see. In fact REPL uses the same JDK to execute code which the compiler is running on. So my suggestion with using -jdk-home will not work because the compiler itself is being run on another JDK. The other approach, with setting the JAVA_HOME environment variable, should work though. Have you tried it?

Hi

I am going to try it. Or alternatively, I will remove newer versions of Java from my machine.

Thank you very much for your help.

Phil

Hi

I set the java to java 8 and it works.

Thank you very much.

Phil