22

I have created a test project aiming to start with Kotlin in IntelliJ IDEA CE.

The IDE version and the system is 2017.1.4 Build #IC-171.4694.23, built on June 6, 2017 JRE: 1.8.0_112-release-736-b21 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Mac OS X 10.12.4

There is one .kt file with a main function in it. When I right-click on it I can select Run, but I get an error form JUnit: !!! JUnit version 3.8 or later expected.

I figured out it was about missing configuration, but I can't set up a proper one for Kotlin as well. Namely it requires a 'main class', which I don't have. Setting package name instead, as some other discussions suggest didn't help either.

What is the proper configuration for Kotlin in IDEA?

Thanks!

2
  • Could you show the class definition including the main function? Maybe there is a mistake in the main function, e.g. not public or static? Commented Jun 18, 2017 at 13:02
  • Yes, it was my mistake, I declared args: String instead of args: Array<String> Commented Jun 18, 2017 at 13:07

6 Answers 6

32

I had a problem where I could not even run the code, IntelliJ IDEA offered me to create a new configuration.

I solved the problem by marking the "src" folder as "Sources" in Project Structure (File > Project Structure, select src and mark it as Sources). For some reason generating new project didn't mark src folder as sources automatically.

Once I did that an arrow appeared next to the main function and I could run it.

Sign up to request clarification or add additional context in comments.

3 Comments

There is no such option as you described
@IgorGanapolsky did you check under the Modules tab in Project Structure window? You should see a file tree on the right, and "Mark as" options above it.
Perfect answer, that's all I need to know. Silly of IntelliJ IDEA to require this.
18

Check that your IDEA is up to date. When creating a new Project you can select Kotlin like this:

Project creation with Kotlin

This will automatically configure Kotlin for you. If you started a Java project and want Kotlin support, there will be a little window in the bottom right corner to configure Kotlin in the project for you. (not needed if the project is created like shown above)

Kotlin configuration in Java project

There you want to select Java, not Javascript and after that click on "Configure '...' module in '...' project as Kotlin (Java) module..."

When your project is set up correctly, make sure that your main function is called "main" and has a parameter of type Array<String>.

fun main(args: Array<String>) {

}

Next to it a Kotlin "K" will appear, which you can click on to run your main function.

Main function execution

4 Comments

It's also a good thing to check if you by any chance put a class Main {at the file (like we do in java), for example. I had that in my file and it wasn't running. After taking it off, the method main was recognized and I could run it.
For me, this doesn't work at all. I've created a new Kotlin project from scratch, but there is no way to run the class and the main method.
There is no Kotlin (JVM) option for new projects
These days if you click "New Project" you can choose whether Java, Kotlin, JS etc
18

Coming from a Java background, I instinctively put the main function inside a class. Then IntelliJ wouldn't let me run it. After taking the main function out of class to the top level, IntelliJ automatically put the green arrow next to the main function and added Run to the context menu (brought up by right clicking the file).

2 Comments

Kotlin doesn't require classes
I had this issue too. Thanks
2

Step-1: Go to: Main Menu -> Project Structure -> Modules
Step-2: Mark "src" folder as "Sources"

Comments

1

Update: It works, I just had to put correct arguments args: Array<String>

1 Comment

Not needed for newer versions of Kotiln: kotlinlang.org/docs/tutorials/kotlin-for-py/hello-world.html
0

The only thing that worked for me was to install the Kotest plugin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.