https://kotlinlang.org logo
Title
v

ValV

07/11/2019, 11:29 AM
I have a class
class MyClass {
  companion object {
    public fun run(args: Array<String>) {
      ...
    }
  }
  public fun main(args: Array<String>) {
    ...
  }
}
How to call
run
from
main
?
d

diesieben07

07/11/2019, 11:32 AM
Just
run(args)
v

ValV

07/11/2019, 11:43 AM
Perhaps, that was a bad idea. Can't run it from jar
d

diesieben07

07/11/2019, 11:54 AM
That
main
fun cannot be used as a JVM main method, those need to be static.
p

Pavlo Liapota

07/11/2019, 11:58 AM
main
should be a top level function and class name would be
<filename> + Kt
then. Or you can put
main
into a companion object of a class, but then you also need annotate this function with
@JvmStatic
.
v

ValV

07/11/2019, 12:02 PM
Many thanks, I've almost broke my brain because of non-runnable jar
πŸ‘ 1
Maybe something wrong with gradle? Even with this trick it does not run
p

Pavlo Liapota

07/11/2019, 12:22 PM
what is error message?
v

ValV

07/11/2019, 12:24 PM
Error: Could not find or load main class testline.ExampleKt
Project tree
src/
β”œβ”€β”€ main
β”‚   β”œβ”€β”€ kotlin
β”‚   β”‚   └── testline
β”‚   β”‚       └── Example.kt
β”‚   └── resources
└── test
    β”œβ”€β”€ kotlin
    β”‚   └── testline
    β”‚       └── ExampleTest.kt
    └── resources
build.gradle.kts:
application {
  // Define the main class for the application.
  mainClassName = "testline.ExampleKt"
}

tasks {
  withType<Jar> {
    manifest {
      attributes(mapOf("Main-Class" to application.mainClassName))
    }
  }
}
d

diesieben07

07/11/2019, 12:29 PM
Can you show
Example.kt
?
v

ValV

07/11/2019, 12:32 PM
package testline

// imports
import java.io.IOException
...
class Example {
  companion object {
    @JvmStatic
    public fun main(args: Array<String>) {
...
    }
  }
}

//fun main(args: Array<String>) {
//  Example.main(args)
//}

// vim: se et ts=2 sw=2 number:
d

diesieben07

07/11/2019, 12:34 PM
When using
companion object
+
@JvmStatic
your
main
method will be in class
Example
, not
ExampleKt
v

ValV

07/11/2019, 12:35 PM
Exception in thread "main" java.lang.NoClassDefFoundError: org/jline/reader/UserInterruptException
	at testline.Example.<clinit>(Example.kt)
Caused by: java.lang.ClassNotFoundException: org.jline.reader.UserInterruptException
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 1 more
Worse
But with
gradle run
it works
I mean with top-level
main
That is commented out
Ok, it's not a secret
Maybe
Thread()
affects?