2

In Kotlin 1.3.30 I just installed, the main entry function doesn't work in command line. JDK is version 8. Intellij Idea is 2019.1.1. No dependency, very clean project.

When I started learning Kotlin, the main entry is 'fun main(args: Array)'. Last year, 'fun main()' could be used without 'args' and I used it a lot. Now I have a project needs command line parameters, but something goes wrong. I've tried 3 formats:

// 1. doesn't work.
fun main(vararg args: String){
    println("this is a test.")
}

// 2. doesn't work
fun main(args: Array<String>){
    println("this is a test.")
}

// 3. this works. but where can I get the command line parameters?
fun main(){
    println("this is a test.")
}

1 & 2 has same response:

λ java -jar test-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
        at com.yxy.ProgramFileKt.main(ProgramFile.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

It works fine with 3 without changing any other thing. But I need command line args.

1 Answer 1

3

Judging by the exception, your jar does not contain the Kotlin stdlib. You need to slightly modify your build.

Adding something like this to your Gradle build should fix it:

dependencies {
  compile "org.jetbrains.kotlin:kotlin-stdlib:1.2.0"
}

P.S. This error is very common and has little to do with the main function. Try googling the error message.

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

4 Comments

... that however wouldn't explain why main without parameters did work... except... there was some stdlib there and now it isn't?
@Roland it does, if you have a look at Intrinsics (github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/…) there are a lot of checks, which probably skipped it main has no args and therefore main without runs without stdlib
Most probably once it encountered the Array or String types in the function declaration, it tried to look for it in the stdlib and couldn't find the stdlib.
Before upgrade to newest IDEA & Kotlin, compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" is added automatically and everything is fine. But with the newest version, implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" was added only. Manual add kotlin-stdlib-jdk8 which include stdlib works.

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.