Anyone have any idea why I can't run a simple main...
# getting-started
m
Anyone have any idea why I can't run a simple main function inside a class? Only if it's a standalone function I can run it but I can't run it if it's inside a class (even if it's given the jvmStatic annotation)
m
You shouldn't use it in Kotlin, even if it may work because of the JVM. This isn't portable for multiplatform and not the Kotlin idiomatic way, because of the boiler plate code.
m
Dude, I need to make a Main class to create a jar, I just needed to test it and I've googled it up and saw this solution to make a Main class for jar
s
I wonder if this is a known bug actually - it seems like in order to use
main
in a companion object as an entry point , you need to have it accept an
Array<String>
(i.e.
@JvmStatic fun main(args: Array<String>)
like the usual java
public static void main(String[] args)
)
also, don’t worry about multiplatform portability if your project isn’t intended to be multiplatform
plenty of us are writing Kotlin exclusively for the JVM
doesn’t make it any less idiomatic
m
@Mr.NiceGuy You don't need a class definition. There will be a class with following scheme: FilenameKt That's the class you want @Shawn This is no bug and made by intention, because you should not use it. It just works because it matches the signature for a JVM main function.
s
Why should you not use it? Can you come up with a reason that an entry point in a companion object should be representable by the language and compilable and not even warned on but still not used?
It might be more idiomatic to write a top-level
main
but it’s hardly anything to chastise a newbie over
m
@molikuner it worked without a class declaration to wrap the main function, thanks
m
@Shawn https://github.com/Kotlin/KEEP/blob/enhancing-main-convention/proposals/enhancing-main-convention.md#entry-points-in-singleton-instances That’s essentially not more than I already said, but maybe it’s more clear in those words.
e
If you used below 1.2, the main requires args: Array<String> as same as Java. (1.3 or above, it is not required)