Skip to content

Confusing error when filename starts with a number -- Exception in thread "main" java.lang.ClassNotFoundException: ... #181

@ivoanjo

Description

@ivoanjo

Hello there, and thanks for kscript!

The other day I bumped onto this issue, and in retrospect it's a bit obvious, but it kind-of got me confused for a few minutes.

Because kscript tries to use the file name of the script as the kotlin class name to be compiled, this fails whenever the filename starts with a number, I'm assuming because that's not a valid Java class identifier name.

Example:

$ cat 1bug.kts 
println("Hello world")

Execution:

$ kscript 1bug.kts 
Exception in thread "main" java.lang.ClassNotFoundException: 1bug
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at Main_1bug$Companion.main(Main_1bug.kt:5)
        at Main_1bug.main(Main_1bug.kt)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.jetbrains.kotlin.runner.AbstractRunner.run(runners.kt:61)
        at org.jetbrains.kotlin.runner.Main.run(Main.kt:109)
        at org.jetbrains.kotlin.runner.Main.main(Main.kt:119)

Could the invalid name be mapped into a simpler name and that one used as a class name? In my case I wanted to use a date as the first part of a file name (so that a bunch of simple scripts were naturally ordered by the date they were written), so it would be nice if this was supported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions