I recently started developing a mobile app with React Native. For a nice IDE experience I installed Webstorm and I did that using Flatpak.

The problem

Unfortunately Flatpak’s sandboxing messes up with programs installed globally in the host system, in particular with Java. And I need my sweet sweet Java to build for Android.

The problem manifests itself with Gradle complaining with the error below:

info Installing the app...

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

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

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
[... stacktrace ...]

This problem may appear when using any IDE installed with Flatpak.

You might be able to avoid it when using IDEA, because it can download its own copy of JDK. With Webstorm I have to rely on the JDK provided by the host system.

The cure

To fix this problem we need to point gradle to use the JDK provided by the host system. Because Webstorm runs in a sandbox, you won’t find java under usual /usr/bin/java nor /usr/lib/jvm/<some-jdk>/bin/java.

Fortunately Flatpak mounts host system’s /etc/ and /usr/ under /var/run/host/etc/ and /var/run/host/usr/ respectively in the sandbox.

So we can set JAVA_HOME environment variable to /var/run/host/usr/lib/jvm/java-11-openjdk-amd64/. I’m using OpenJDK 11, as you can see.

To find out your JDK location you can check it using ls -l $(which java). In my case I had to jump through 2 symlinks to find it: which java -> /usr/bin/java -> /etc/alternatives/java -> /usr/lib/jvm/java-11-openjdk-amd64/bin/java

Applying it in Webstorm

If you use React Native run configuration in Webstorm:

  1. Go to “Edit Run Configuraions”
  2. Go to your React Native target configuration
  3. Add the following line in “Environment variables:” field

If you use terminal window to run your RN app:

  1. Go to Settings > Tools > Terminal
  2. Add the above JAVA_HOME line in “Environment variables:” field

👋 Hope this helps, Jan