Home > OS >  HelloWorld in Kotlin gives error "Could not find or load main class"
HelloWorld in Kotlin gives error "Could not find or load main class"

Time:10-11

I spent the last 1,5 hour trying to make enter image description here

It also complains Kotlin is not configured, but I have already configured it.

Here's the run configuration: enter image description here

Here are the project settings: enter image description here

CodePudding user response:

Your Hello.kt file needs to be somewhere inside the src/main folder, probably in src/main/kotlin. This is different from the tutorial, because your project is using Gradle, and the one in the tutorial isn't. I think this is because newer versions of IntelliJ use Gradle by default for new projects, which wasn't the case when the tutorial was written.

The use of src/main/kotlin and src/test/kotlin as source code directories is a convention in Gradle (and Maven). When importing a Gradle project into IntelliJ, main becomes a module, and kotlin becomes a content root within that module. The same goes for test. In your screenshots, the bold text and blue icons on main and test confirm that's how your project is set up. Files outside of those folders aren't treated as source files, which explains why your Hello.kt file isn't being compiled or recognised correctly.

It's likely that the default behaviour of IntelliJ when creating a new project has changed since this tutorial was written. In the tutorial, they select "Kotlin" as the project type and this creates a project that doesn't use Gradle. As a result, the project doesn't use the src/main/kotlin directory structure.

I can see from your video that you selected the same option, but on the next screen, IntelliJ still automatically selected Gradle as the build system for the new project. To match the project structure used in the tutorial, I think you would need to select "IntelliJ" as the build system.

  • Related