how may I execute a native command and retrieve it...
# kotlin-native
e
how may I execute a native command and retrieve its output? This returns an empty string instead of
Copy code
elect@5800x:~/IdeaProjects/java-launcher$ echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-amd64
a
I'd guess JAVA_HOME isn't set in the context of the native program. What happens if you try
echo blah
instead?
e
but on a terminal is.. could be the kotlin native environment is different?
a
it could be
e
indeed, you are right
"echo JAVA_HOME" -> JAVA_HOME
uhm, this is disappointing
I can see that
Copy code
printenv
isn't empty (list of all envs), but
JAVA_HOME
isn't there unfortunately..
a
environment variables are a black magic to me, I know it's really hard to set a global env var on macOS, but if you're using Linux or Windows it should be easier
alternatively you can use Gradle and try to pass in an environment variable
Copy code
import org.jetbrains.kotlin.gradle.plugin.mpp.Executable
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

kotlin {
  targets.withType<KotlinNativeTarget>().configureEach {
    binaries.withType<Executable>().configureEach {
      runTaskProvider?.configure {
        environment("JAVA_HOME", System.getProperty("java.home"))
      }
    }
  }
}
(not tested, but I've used something similar before)
e
null
just to be sure,
Executable
is from
org.jetbrain.kotlin.gradle.plugin.mpp
?
a
yes (I added the imports above)
maybe JAVA_HOME is null in your build script too 🙃
😂 1
if you run the Gradle command from your shell it should pass the JAVA_HOME value through...
e
uhm, better
/home/elect/.jdks/openjdk-19.0.1
but still not the above
fine
a
I guess you're running with IntelliJ, and it's configured to use jdk19
e
yep
I'm using the gui, running the gradle
runDebugExecutableNative
you know what, from running via
./gradlew rDEN
it works flawless,
JAVA_HOME
is properly read.. the issue rises up only via Idea gui..
a
what OS are you using?
e
Ubuntu
a
have you tried using jenv or setting a global environment variable? https://askubuntu.com/questions/261760/setting-global-environment-variable-for-everyone
e
no, not yet but I'm fine having it working from CLI