Could not find or load main class MainKt

Dear Community,

By way of context I am a beginner in Kotlin and Intellij IDEA. Based on my online search, no matter what I do I am not able to resolve the following error. I have already explored many of the suggestions in StackOverflow and none seem to work.

Error: Could not find or load main class MainKt
Caused by: java.lang.ClassNotFoundException: MainKt

I am using a Windows 11 laptop, where I don’t have the admin rights. I have installed Intellij IDEA using the Toolbox application.

May I kindly, request your support (kindly, do consider that I have already applied most of the answers on Stackover flow like rebuilding the project, reinstalling IntelliJ, etc.)


I suggest using Gradle (or Maven) as otherwise it is hard to say what’s your project config exactly. According to the log, it seems you don’t at all load the code of your project when running it. It mentions stdlib and annotations.jar, but no classes from the project. Why? I have no idea - again, it depends on the setup of your project in IntelliJ.

Dear broot, Thanks for your input and explanation. Being a beginner I believe I will have to do some search and understand it better. Any good resource on properly setting up a project and trouble shooting with Gradle will be really helpful.
Thanks again for your support.

You can use IntelliJ itself to create a new Gradle project; when you create a new project with IntelliJ, it gives you options for what kind of base project you want to create, so you can create a Kotlin-only project with no build tool, or you can create a Maven project using Kotlin, or a Gradle project using Kotlin, or even something else entirely.

1 Like

Or open a terminal in an empty directory and type
gradle init

Choose 2: Application
Choose 4: Kotlin
Choose 1: dont split across multiple projects
Choose your build script language. Groovy has more online answers, Kotlin is catching up
Choose ‘no’ for new api (that’s what I do at least)
Choose whatever test framework you want.
Choose a project name (defaults to directory name)
Choose a base package name (defaults to directory name)

You now have a kotlin project that you can run with
./gradlew run

You can open this project in intellij if you wish.

1 Like