我正在尝试通过 bash 执行命令,例如 konanc
.
在 KotlinJVM 中,这只会使用 Runtime.getRuntime().exec("...")
,或创建 Process
使用ProcessBuilder
但是,这些类在 Kotlin-Native 中都不可用,因为它们是 Java 库的一部分。
我尝试在文档和 kotlin-native GitHub 存储库中搜索示例代码,但没有找到任何内容。
最佳答案
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]}")
}
使用 ./file <name>
调用它将创建一个以参数 name
命名的文件在当前目录中。
关于Kotlin native - 执行可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460921/