shaft
shaft

Reputation: 2229

What is required to run a "Hello, world!" in Kotlin with IntellJ IDEA?

I have a Mac (macOS Catalina) with Java 15 and IntelliJ IDEA 2020.2 (Community Edition) installed. The Kotlin plugin is also installed. When I create a new Kotlin Project, add a main function, then there is way to run it, there is no 'Start' Button next to the main function.

The problem is obviously that it doesn't find the runtime. The .iml file looks as follows

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" name="KotlinJavaRuntime" level="project" />
  </component>
</module>

When I click on 'add framework support', I can add a new KotlinJavaRuntime which then just appends an <orderEntry> to the .iml file with the name KotlinJavaRuntime (2).

I don't want to use Maven or Gradle here. I know that Gradle can resolve the dependencies for me. I just want a plain vanilla Kotlin "Hello, world!" project. The documentation has a 'Getting Started with IntelliJ IDEA' section which just mentions the Kotlin plugin for IntelliJ. There seems to be no requirement to have Gradle or Maven installed, the plugin should do it.

I know that you can use brew install kotlin to install the SDK manually, but then the documentation is wrong and the plugin alone cannot run Kotlin with a prior manual installation of the SDK or the usage of Maven/Gradle.

Upvotes: 2

Views: 280

Answers (2)

shaft
shaft

Reputation: 2229

I think I know what happened in this case.

The Kotlin Code was loaded as an 'IntelliJ' Module inside a Java project that was using maven. In IntelliJ there's a hidden folder .idea/libraries/ which was missing the description of the Kotlin Runtime (this makes sense, since the dependencies were managed via maven). I have restarted and created a new project from scratch. This time you'll see the file .idea/libraries/KotlinJavaRuntime.xml, which is exactly what is referenced from the iml file. The .idea folder is very magic. For me the confusion is that modules in IntelliJ are not really isolated from the project, they share - like in this case - the .idea folder.

I hope this helps also other users.

Upvotes: 1

Simon Danninger
Simon Danninger

Reputation: 458

If you created the kotlin-file yourself, you have to tell IntelliJ that it should run this file.

Normally shows a run icon next to your main method. run button/icon

Option 2: Right Click on the file containing the main method and see if there is a run this file option.

Option 3: add run config manually

  1. Click add config which should be on the left of the run button (don't mind that it is disabled)
  2. Click + and choose Kotlin.
  3. Set main class to MainKt which is the default for kotlin

Upvotes: 0

Related Questions