Jeff
Jeff

Reputation: 99

In an IntelliJ Kotlin project, when I try to run a java class, I get a "Could not find or load main class" error

I try to run the class from the green arrow in IntelliJ and I get the "Could not find or load main class" error. If I try to run the class from the terminal by running

javac <filename.java>
java <filename>

it works. I tried to do "rebuild project" and to make a new project and it did not change anything.

Here is a photo of my project:

enter image description here

Here is a photo of my Run-Edit configurations

enter image description here

Upvotes: 1

Views: 1756

Answers (1)

CrazyCoder
CrazyCoder

Reputation: 402345

Your java code needs to be placed in src/main/java directory instead of src/main/kotlin.

Kotlin compiler doesn't compile Java files in Kotlin source roots, Java compiler doesn't compile Java files in Kotlin source roots, therefore .class files are not created by any of the compilers and you get this error.

The solution is to move *.java files into src/main/java directory. Create this directory manually if it doesn't exist.

Upvotes: 10

Related Questions