1

I am new to kotlin and I am trying to use main function together with a class.

fun main() {
    var demo = Person("Hello", 10)

    println(demo)
}

private class Person (name: String, age: Int){
    var name: String
    var age: Int

    init {
        this.name = name
        this.age = age
    }
}

Although I have declared the main function, the compiler is still looking for the static main method in the class that I have defined, Person, and I got this error:

Error: Main method not found in class Person, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

What have I missed?

John Winston
  • 1,260
  • 15
  • 30

1 Answers1

0

java Person is the problem, you are specifically telling it to look for the static void main method there. Top-level functions from Person.kt will be compiled into a PersonKt class by default and so you should run java PersonKt.

Also, the unnamed package you get by not specifying any package is weird in some ways, so I would recommend adding a package declaration from the start (and running java your.package.PersonKt).

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487