Kotlin native - 执行可执行文件

Mar*_*cel 6 shellexecute exec kotlin kotlin-native

我正在尝试通过 bash 执行命令,例如konanc.

在 KotlinJVM 中,这只是使用Runtime.getRuntime().exec("...")或创建Process使用ProcessBuilder,但是,这些类在 Kotlin-Native 中都不可用,因为它们是 Java 库的一部分。

我尝试在文档和 kotlin 原生 GitHub 存储库中搜索示例代码,但没有找到任何内容。

小智 4

tl;dr 不,kotlin-native 没有标准的流程 api

好吧,原生的 kotlin std 仍在开发中,我认为 process api 不会很快出现。

但是,您可以使用与某些 C 进程库的互操作性,例如https://github.com/eidheim/tiny-process-library

您可以在这里找到操作方法https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md

不过,您还可以使用 POSIX 的 exec/fork 调用来生成和创建新进程,并且我认为 kotlin-native 确实包含适用于 linux/windows 的 POSIX。https://github.com/JetBrains/kotlin-native/tree/master/platformLibs/src/platform请参阅 posix.def 了解平台。

例子:

import platform.posix.*

fun main(arguments: Array<String>) {
    println("${arguments[0]}")
    execlp("touch", "touch", "${arguments[0]}")
}
Run Code Online (Sandbox Code Playgroud)

调用它会在当前目录中./file <name>创建一个以参数命名的文件。name