I recently started developing a mobile app with React Native. For a nice IDE experience I installed Webstorm and I did that using Flatpak.
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.
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
Fortunately Flatpak mounts host system’s
/var/run/host/usr/ respectively in the sandbox.
So we can set
JAVA_HOME environment variable to
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:
- Go to “Edit Run Configuraions”
- Go to your React Native target configuration
- Add the following line in “Environment variables:” field
If you use terminal window to run your RN app:
- Go to Settings > Tools > Terminal
- Add the above
JAVA_HOMEline in “Environment variables:” field
👋 Hope this helps, Jan