Resolving the “JAVA_HOME is not set” Error in React Native

Resolving the "JAVA_HOME is not set" Error in React Native

17 December 2024 Stephan Petzl Leave a comment Tech-Help

When working with React Native, especially on a clean Linux installation, you might encounter an error stating that JAVA_HOME is not set, and no ‘java’ command could be found in your PATH. This issue arises because the latest versions of Android Studio use an internal Java Runtime Environment (JRE), which is not automatically included in system paths.

Understanding the Problem

The error typically occurs when running the command:

react-native run-android

This command attempts to build and install the app on a connected device or emulator. However, it fails because the system cannot locate the Java installation required for the build process.

Solution: Setting Up JAVA_HOME

For Windows Users

  1. Open Android Studio and navigate to File → Other Settings → Default Project Structure…
  2. Note the JDK location displayed, such as C:\Program Files\Android\Android Studio\jre.
  3. Open the System Properties by right-clicking on “This PC” and selecting Properties → Advanced System Settings → Environment Variables…
  4. Create a new system variable:
    • Variable name: JAVA_HOME
    • Variable value: Path to the JRE, e.g., C:\Program Files\Android\Android Studio\jre
  5. Confirm by opening a new command prompt and typing echo %JAVA_HOME% to verify the path.

For Linux Users

  1. Locate the Android Studio installation directory and find the /jre folder.
  2. Edit your .bashrc or .bash_profile file by adding the following line:
    export JAVA_HOME=/jre
  3. Reload the terminal configuration with the command:
    source $HOME/.bash_profile

    or

    source $HOME/.bashrc
  4. Ensure an Android emulator is running or connect a real device to execute the React Native application.

Alternative Approach: Running the Project in Android Studio

If you prefer not to set up a separate Java installation, you can open the Android project directly in Android Studio:

  • Navigate to the android directory of your React Native project.
  • Open this directory as a project in Android Studio.
  • Use Android Studio’s built-in tools to run the project on an emulator or connected device.

For seamless JavaScript code updates, enable live reload in the emulator by pressing CTRL + M (or CMD + M on macOS) and selecting “Enable live reload”.

Enhancing Development with Repeato

While configuring your development environment, consider using Repeato, a no-code test automation tool for React Native. Repeato allows you to create, run, and maintain automated tests effortlessly. Its computer vision and AI capabilities make test editing and execution swift and efficient, providing an excellent solution for ensuring app quality without extensive setup.

Like this article? there’s more where that came from!