Hey there! Is it possible to get the program invoc...
# kotlin-native
m
Hey there! Is it possible to get the program invocation in Kotlin Native? Let’s say I have a program called
binary
: 1. If the program is named
binary
, I’d like to get that in Kotlin code 2. If the program is renamed to
othername
and executed, I’d like to get
othername
Is that possible in Kotlin Native? PS: Basically this is available as
argv[0]
in C/C++, but doesn’t seem to be available in Java programs.
l
Kotlin Native’s main function can take in args: Array<String>, which gives you access to the arguments.
m
yes, but it does only print the arguments, not the program name
So this example:
Copy code
fun main(args: Array<String>) {
    println(args.toList())
}
Compiled with
kotlinc-native -o args args.kt
:
Copy code
$ ./args.kexe one two three
[one, two, three]
It will not contain
./args.kexe
or
args.kexe
l
That’s interesting. I wonder why it’s different.
m
well - I guess it’s to be consistent to Java, where the program name is removed from args as well
at least I thought there would be a way to access this info
r
I think you should open an issue. If there is a way to get this info, it would be documented as an answer to this issue and if not, you could justify your needs and maybe get it implemented
m
n
If the Kotlin Native program is running on Linux then the Proc file system (mounted as /proc) can be used to obtain the program name (including the program's absolute path).
Kotlin Native programs have some advantages over JVM programs when it comes to platform integration (access to platform APIs, can deploy everything easily as a single self contained binary, easy central process control etc), especially with Linux/Embedded Linux.
m
Yes, that’s possible of course - but a whole lot of indirection and hoops to get something that should be available by default 🙂