Unable to run Kotlin program from command line
Answered
I have built and tested a command line program in the IDE. I used the artifact build to make a command line version.
When I run the program I get the following result.
java -jar FirstLayer.jar
no main manifest attribute, in FirstLayer.jar
I am assuming the problem is with the manifest file.
The MANIFEST.MF file from jar file contains:
Manifest-Version: 1.0
Implementation-Title: kotlin-stdlib-jdk7
Kotlin-Runtime-Component: Main
Kotlin-Version: 1.8
Implementation-Version: 1.8.0-release-345(1.8.0)
Multi-Release: true
Implementation-Vendor: JetBrains
The manifest file in src/main/resources/META-INF contains
Manifest-Version: 1.0
Main-Class: MainKt
Main-Class: MainKt
When I created the artifact I specified the manifest file in resources.
If there is no way to fix this, how would I specify the main class java command line.
Please sign in to leave a comment.
It looks like you created the jar artifact without specifying the main class. You can remove the existing jar, and re-create it following this guide: https://www.jetbrains.com/help/idea/create-your-first-kotlin-app.html?section=Maven#package-as-jar
Pay attention to the main class configuration step: https://i.imgur.com/KL0Zh0r.png
Also, it is possible to run the existing jar by specifying the main class via classpath:
java -cp FirstLayer.jar MainKtIn any way I recommend you to use a build/dependency management tool like maven or gradle for easier and faster building/creating artifacts etc.
Thanks for the reply.
I did build the artifact in Intellij using gradle. I did another test and made sure the main class was configured. I found that MAINFEST.MF in the jar file is incorrect. If I use zip to replace the manifest with the correct MANIFEST.MF
Manifest-Version: 1.0
Main-Class: MainKt
the file will run with java -jar FirstLayer.jar.