"ERROR: JAVA_HOME is set to an invalid directory" for android build

(cli) openfl build android
(long logs)

ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

I have already tried setting $JAVA_HOME to “/usr/lib/jvm/default-java” and “/usr/lib/jvm/java-17-openjdk-amd64/bin/java”.

But interestingly. The program still keeps insisting my “JAVA_HOME” env variable is set to “/usr/lib/jvm” even thought it’s not.

I have already closed and re-opened my terminal.

I have already tried “source ~/.bashrc” with JAVA_HOME being set to several different paths, and still nothing.

I have tried to “force it” like: “JAVA_HOME=(path…); openfl build android” but it seems it’s “getting” the env variables from somewhere else.

The “.env” file in the current directly does not exist so it can’t be it.

Here are some program output to help:

Vik@Vik: pts/0: 7 files 36Kb ❯ java --version
openjdk 17.0.1 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-Ubuntu-121.04)
OpenJDK 64-Bit Server VM (build 17.0.1+12-Ubuntu-121.04, mixed mode, sharing)

Vik@Vik: pts/0: 7 files 36Kb ❯ cat Build.xml
<xml> <set name="HXCPP_ARM64" value="1" if="android" /> <files id="hello"> <file name="Hello.cpp" /> </files> <target id="default" output="hello" tool="linker" toolid="${STD_MODULE_LINK}"> <outdir name="bin" /> <ext value="" /> <files id="hello" /> </target> </xml>

Vik@Vik: pts/0: 7 files 36Kb ❯ cat project.xml
<?xml version="1.0" encoding="utf-8"?> <project> <meta title="HelloWorldProject" package="com.sample.helloworldproject" version="1.0.0" company="Company Name" /> <app main="Main" path="Export" file="HelloWorldProject" /> <source path="Source" /> <haxelib name="openfl" /> <config:android build-tools-version="1" /> <assets path="Assets" rename="assets" /> </project>

Vik@Vik: pts/0: 7 files 36Kb ❯ haxe --version
4.1.5

Vik@Vik: pts/0: 7 files 36Kb ❯ echo $JAVA_HOME
/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Thanks.

“lime setup android” stores settings here ~/.lime/config.xml
check the file

Thanks it worked.

But now I get the error:

“Failed to find target with hash string ‘android-30’ in: /opt/android-sdk”

Not sure what todo.

I’m very close I can feel it.

The answers on this Stack Overflow question about that error message seem to indicate that you need to open Android Studio’s SDK manager and download the Android SDK platform for API level 30.

I’m getting this now:
ERROR: JAVA_HOME is set to an invalid directory: $/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home

Using openfl 9.4.0 & lime 8.2.1.
In my lime config.xml I have ANDROID_SDK set properly and using NDK r21e as per the lime docs. Also I’m on macOS. I got it running last year lol but can’t seem to get it to build successfuly for Android now.

Could it be that you accidentally added an extra $ character at the beginning of the JAVA_HOME path?

In my ~/.lime/config.xml file, I have this value for JAVA_HOME (I’m using Temurin OpenJDK from adoptium.net):

<define name="JAVA_HOME" value="/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home" />
1 Like

Thank you. It was that typo. This answer helped me also.

Maybe setting the JAVA_HOME location in the config.xml should be be part of lime setup android? That line didn’t exist in my config until I manually added it in.

Looking at Lime’s source code, it appears to skip asking for JAVA_HOME when setting up Android on macOS only. It will ask to configure JAVA_HOME when setting up Android on other operating systems.

That code is from before my time, so I can’t say for sure why we decided that it’s okay for JAVA_HOME to be empty on macOS. If I had to guess, we may be assuming that /usr/bin/java will generally just work without extra configuration on macOS. On Windows, Java doesn’t have a universal executable path like that.

1 Like