Doesn't this take away the feature of having multiple main entry points in java which can be called as and when required.
Asked
Active
Viewed 4,918 times
2 Answers
23
In addition to Sergey Mashkov's comment: you can put a main
inside an object and mark it @JvmStatic
:
object Main {
@JvmStatic
fun main(args: Array<String>) {
println("Hello, world!")
}
}

Andrey Breslav
- 24,795
- 10
- 66
- 61
-
Yes but beware, if you do this, you will not be able to use most of the features in `kotlin.reflect`, because not all of the classes will be loaded. – SMMH Jun 02 '22 at 14:32
22
UPDATE: recent versions of Kotlin allow multiple main
functions even in the same package (if they are in different files).
You can have multiple main functions in your project but only one main function per package
The reason why you can't make multiple main functions in package is that all functions in package are stored in Package class so you can't have multiple functions in a class with same signatures.
So if you want multiple main functions you have to define them in different packages

Jemshit
- 9,501
- 5
- 69
- 106

Sergey Mashkov
- 4,630
- 1
- 27
- 24
-
1You can now have a main per Kotlin file. Kotlin no longer generates a package level container class for all of the top-level functions, and instead creates a class per file. If the filename is `App` and is in package `foo.bar` with your `main()` function, then the class name will be `foo.bar.AppKT` containing that `main()` – Jayson Minard Jan 02 '16 at 02:55
-
-
If you do, then all but one main function has to be private, otherwise there will be a name clash. Or am I missing something? – Duncan McGregor Sep 17 '17 at 15:02