https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
e

elect

12/12/2023, 5:33 PM
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

Adam S

12/12/2023, 5:40 PM
I'd guess JAVA_HOME isn't set in the context of the native program. What happens if you try
echo blah
instead?
e

elect

12/12/2023, 5:41 PM
but on a terminal is.. could be the kotlin native environment is different?
a

Adam S

12/12/2023, 5:41 PM
it could be
e

elect

12/12/2023, 5:41 PM
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

Adam S

12/12/2023, 5:46 PM
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

elect

12/12/2023, 5:47 PM
null
just to be sure,
Executable
is from
org.jetbrain.kotlin.gradle.plugin.mpp
?
a

Adam S

12/12/2023, 5:49 PM
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

elect

12/12/2023, 5:52 PM
uhm, better
/home/elect/.jdks/openjdk-19.0.1
but still not the above
fine
a

Adam S

12/12/2023, 5:54 PM
I guess you're running with IntelliJ, and it's configured to use jdk19
e

elect

12/12/2023, 5:54 PM
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

Adam S

12/12/2023, 10:18 PM
what OS are you using?
e

elect

12/13/2023, 8:41 AM
Ubuntu
a

Adam S

12/13/2023, 8:43 AM
have you tried using jenv or setting a global environment variable? https://askubuntu.com/questions/261760/setting-global-environment-variable-for-everyone
e

elect

12/13/2023, 8:46 AM
no, not yet but I'm fine having it working from CLI