Kotlin Native equivalent of System.exit(-1)
Asked Answered
F

1

13

In the following Kotlin/JVM program System.exit(-1) stops the execution of the program with an error exit code:

fun main(args: Array<String>) {
    if (args.size < 2) {
        println("too few args!")
        System.exit(-1)
    }
    println("Hello, ${args[1]} from ${args[0]}")
}

Kotlin/Native does not have access to any Java classes, including System. So what would the equivalent function be for a Kotlin/Native program to stop execution of the program with an error code?

Froghopper answered 2/12, 2017 at 3:29 Comment(1)
I guess you could normal exit function from C.Reunite
H
20

Use exitProcess:

import kotlin.system.exitProcess
...
exitProcess(exitCode)

Declaration and documentation in the Kotlin source code.

Hong answered 29/1, 2018 at 13:46 Comment(3)
Hmm, this is not available in commonMain. Is there an import missing? The docs link is also broken.Fissirostral
@Fissirostral Should still be valid. Not sure though, haven't used Kotlin in a couple yearsHong
I found the new link (repo was moved), updating now. But still didn't work for me, it's not resolving the system package :/Fissirostral

© 2022 - 2024 — McMap. All rights reserved.