Repository: JetBrains/kotlin
File: compiler/cli/cli-runner/src/org/jetbrains/kotlin/runner/Main.kt (Kotlin)

Code before:
            }
            else if (arg.startsWith("-")) {
                throw RunnerException("unknown option: $arg")
            }
            else if (howtorun == HowToRun.JAR || (howtorun == HowToRun.GUESS && arg.endsWith(".jar"))) {
                setRunner(JarRunner(arg))
                restAsArguments()
                break
            }
            else if (howtorun == HowToRun.SCRIPT || (howtorun == HowToRun.GUESS && arg.endsWith(".kts"))) {
                setRunner(ScriptRunner(arg))
                restAsArguments()
                break
            }
            else {
                val workingDir = File(".")
                val classFile = File(arg)
                // allow running class files with '.class' extension, but in order to infer its fully qualified name,
                // it must be located in the working directory or a subdirectory of it
                val className =
                    if (arg.endsWith(".class") && classFile.exists() && classFile.canonicalPath.contains(workingDir.canonicalPath)) {
                        classFile.canonicalFile.toRelativeString(workingDir.canonicalFile).removeSuffix(".class")
                            .replace(File.separatorChar, '.')
                    } else {
                        arg
                    }
                setRunner(MainClassRunner(className))
                restAsArguments()
                break
            }
            i++
        }

        if (classpath.isEmpty()) {
            classpath.addPath(".")
        }

        if (!noStdLib) {
            classpath.addPath("$KOTLIN_HOME/lib/kotlin-stdlib.jar")
        }

        if (!noReflect) {
            classpath.addPath("$KOTLIN_HOME/lib/kotlin-reflect.jar")
        }

        if (runner == null) {
            setRunner(ReplRunner())
        }

        if (runner is RunnerWithCompiler && compilerClasspath.isEmpty()) {
            findCompilerJar(this::class.java, KOTLIN_HOME.resolve("lib")).forEach {

Review comment (suggestion):
The method `toRelativeString` may throw an `IllegalArgumentException` when our `classFile.canonicalFile` and `workingDir.canonicalFile` have different roots, but it has been circumvented in our `if` statement,  so there is no need to catch it.

Code after:
            }
            else if (arg.startsWith("-")) {
                throw RunnerException("unknown option: $arg")
            }
            else if (howtorun == HowToRun.JAR || (howtorun == HowToRun.GUESS && arg.endsWith(".jar"))) {
                setRunner(JarRunner(arg))
                restAsArguments()
                break
            }
            else if (howtorun == HowToRun.SCRIPT || (howtorun == HowToRun.GUESS && arg.endsWith(".kts"))) {
                setRunner(ScriptRunner(arg))
                restAsArguments()
                break
            }
            else {
                val workingDir = File(".")
                val classFile = File(arg)

                // Allow running class files with '.class' extension.
                // In order to infer its fully qualified name, it should be located in the current working directory or a subdirectory of it
                val className =
                    if (arg.endsWith(".class") && classFile.exists() && classFile.canonicalPath.contains(workingDir.canonicalPath)) {
                        classFile.canonicalFile.toRelativeString(workingDir.canonicalFile)
                            .removeSuffix(".class")
                            .replace(File.separatorChar, '.')
                    } else arg

                setRunner(MainClassRunner(className))
                restAsArguments()
                break
            }
            i++
        }

        if (classpath.isEmpty()) {
            classpath.addPath(".")
        }

        if (!noStdLib) {
            classpath.addPath("$KOTLIN_HOME/lib/kotlin-stdlib.jar")
        }

        if (!noReflect) {
            classpath.addPath("$KOTLIN_HOME/lib/kotlin-reflect.jar")
        }

        if (runner == null) {
            setRunner(ReplRunner())
        }

        if (runner is RunnerWithCompiler && compilerClasspath.isEmpty()) {