6

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.

Marcel
  • 1,232
  • 1
  • 14
  • 32

1 Answers1

2

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.

Marcel
  • 1,232
  • 1
  • 14
  • 32
PolishCivil
  • 111
  • 2
  • 12
  • Thanks for the answer, i have provided a working example. – Marcel Jul 22 '18 at 00:25
  • any code after execlp doesn't get executed, is there a workaround? – rifaqat Nov 12 '18 at 12:25
  • println("Hello"), execlp("java", "/usr/bin/java", "-version"), println("Done") . ---- the program ends with printing 'Done'... – rifaqat Nov 12 '18 at 12:27
  • Sorry, for the late answer, but I assume that's because execlp waits for the child process to exit. If the process is something like a browser for example, it'll probably block til the application is closed. – Marcel Feb 05 '21 at 11:59