I have a function like this at top-level of a Kotl...
# getting-started
m
I have a function like this at top-level of a Kotlin file:
Copy code
package mypackage

fun main(args: Array<String>) {
    // ...
}
Usually used as program entrypoint in JVM. However, when I try to call it from another Kotlin module:
Copy code
import mypackage.main

class MyClass {
    fun myMethod() {
        main(arrayOf("foo", "bar"))
    }
}
I get this strange error:
Copy code
Kotlin: Overload resolution ambiguity: 
public fun main(args: Array<String>): Unit defined in mypackage
public fun main(args: Array<String>): Unit defined in mypackage
Ah, I think I found the reason, there are two files with a
main
fun defined in the same package. That's not a problem when running the programs, since then end up in different JVM classes (with
Kt
suffix). And not a problem when calling from Java. But how can I call from Kotlin?
j
Maybe resolve the ambiguity in your declarations, for example give them different names, and if you really need entrypoints, add
main()
functions that call those.
m
The problem is that I do not control the code of the library I am calling.
j
But why would a library have
main()
functions?
m
Since it is primarily used as CLI, and I am misusing it as a library.
Anyway, I just want to know if there is any reasonable workaround without changing the "library" I try to call.
One workaround would be to introduce a small bridge in Java, another would be to use reflection. I don't like either of those.
j
Another option would be to directly call the classes that the main function of the library calls (essentially inclining
main
).
Yet another one would be to call the CLI as a separate process from Kotlin using
ProcessBuilder
(this would be the "proper way" to use the CLI application)
m
The problem is that the
main
methods use private methods, so I cannot inline it. 🤷
👌 1