I made a kotlin console application and I can run ...
# intellij
j
I made a kotlin console application and I can run it just fine inside Intellij IDE but when I build the jar file and try to run it I get an error saying
Error: Could not find or load main class MainKt
I followed the Creating your first java application article on the jetbrains site so I dont know what the problem is. I created a StackOverflow question with more details
m
To troubleshoot, you can unzip the jar file (a jar file is a zip) and look for a MainKt.class file inside
I don't see anything wrong in the setup so it should theoretically be there.
Maybe try replacing
args: Array<String>
with
args: List<String>
I don't think that'd be an issue but maybe the jvm expect a List?
If there's a MainKt.class file, you can decompile the bytecode with
javap
to make sure the main method is there
j
@mbonnin If I go into
build/classes/kotlin/main/com/mypackage/
in the IDE there is a
MainKt.class
file and it looks like this
Copy code
public fun main(args: kotlin.Array<kotlin.String>): kotlin.Unit { /* compiled code */ }
m
Then it looks like your
MainKt
class is in the
com.mypackage
package?
Try
mainClassName = 'com.mypackage.MainKt'
?
But if you really want to make sure the jar is good, it's better to unzip it directly
j
Looking through the documents more I found this tutorial so I added the jar task
Copy code
tasks.jar {
    manifest {
        attributes 'Main-Class': 'MainKt'
    }
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}
and deleted my other artifact coniguration and I dont get that error anymore but now I get a
NoClassDefFoundError
for the Mqtt library I am using but still runs fine in the Intellij