Search code examples
kotlinexecshellexecutekotlin-native

Kotlin native - Execute an executable


I am trying to execute a command via bash, for example konanc.

In KotlinJVM this would just be using Runtime.getRuntime().exec("..."), or creating a Process using the ProcessBuilder, however, none of those classes are available in Kotlin-Native, as they are part of the Java libraries.

I tried searching for example code in the documentation and the kotlin-native GitHub repository, but haven't found anything.


Solution

  • tl;dr No, there is no standard process api for kotlin-native

    Well the kotlin std for native is still under development, and i don't think process api will be there anytime soon.

    However you could use interoperability with some C process library such as https://github.com/eidheim/tiny-process-library

    How-to you will find here https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md

    However there are also POSIX's exec/fork calls you could use to spawn and create new process and i think kotlin-native does include POSIX for linux/windows. https://github.com/JetBrains/kotlin-native/tree/master/platformLibs/src/platform see posix.def for platforms.

    Example:

    import platform.posix.*
    
    fun main(arguments: Array<String>) {
        println("${arguments[0]}")
        execlp("touch", "touch", "${arguments[0]}")
    }
    

    Calling it with ./file <name> will create a file that is named after the parameter name in your current directory.