3

I'm trying to load kotlin.String using SystemClassLoader

        inputClassName = String::class.qualifiedName.toString()
        val input = ClassLoader.getSystemClassLoader()
            .loadClass(inputClassName) // here I get error

and I'm getting error. Stack trace:

Exception in thread "main" java.lang.ClassNotFoundException: kotlin.String
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
    at block.execution.Executor.init(Executor.kt:40)
    at block.AppKt.main(App.kt:50)

What am I doing wrong? How can I fix it?

4
  • It sounds like you are missing the kotlin dependencies from your runtime classpath. Commented May 3, 2020 at 3:40
  • 2
    I'm just curious, why there's a need to load String once there's already String::class? Commented May 3, 2020 at 4:26
  • @StephenC No, this will happen even if Kotlin dependencies are in the classpath. Commented May 3, 2020 at 7:58
  • 1
    @MikhailKopylov this is just a simplified example. I need to dynamically load classes that the users asks for Commented May 4, 2020 at 14:00

1 Answer 1

4

kotlin.String doesn't exist as a class which can be loaded, it is mapped to java.lang.String by the compiler. String::class.java.name will give you the actual name of the class.

Of course, as Mikhail Kopylov points out, there is little point loading a class by name of an already loaded class. Maybe to load it in a specific ClassLoader, but String will definitely be loaded by the system class loader anyway.

Sign up to request clarification or add additional context in comments.

1 Comment

Then consider accepting it (see stackoverflow.com/help/someone-answers).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.