Flutter / Dart cannot find java and $JAVA_HOME

8,439

As I just installed everything, from java through android, flutter, dart etc. and I just set my .bashrc and /etc/profile, I just needed a clean restart on the system to make it work. Probably something about how IDEA gets env variables.

Share:
8,439
ForestG
Author by

ForestG

Hungarian Frontend developer, currently working in Angular on multiple projects. Has some experience with Datamining, backend Java, cross-platform and JS based full stack. While not debugging buttons, I love to jog, hike, and videogames (told you mom it would stick).

Updated on December 25, 2022

Comments

  • ForestG
    ForestG over 1 year

    I am following the official tutorial on flutter.dev but I get the following error when clicking "run" in IntelliJ IDEA:

    Launching lib/main.dart on sdk gphone x86 arm in debug mode...
    Running Gradle task 'assembleDebug'...
    
    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.
    
    Exception: Gradle task assembleDebug failed with exit code 1
    

    If I run the flutter doctor -v you can see that it's installed:

    $ flutter doctor -v
    [✓] Flutter (Channel stable, 1.22.1, on Linux, locale en_US.UTF-8)
        • Flutter version 1.22.1 at /home/forest/snap/flutter/common/flutter
        • Framework revision f30b7f4db9 (11 days ago), 2020-10-08 10:06:30 -0700
        • Engine revision 75bef9f6c8
        • Dart version 2.10.1
    
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
        • Android SDK at /home/forest/Android/Sdk
        • Platform android-30, build-tools 30.0.2
        • ANDROID_HOME = /home/forest/Android/Sdk
        • Java binary at: /home/forest/.jdks/jdk1.8.0_261/bin/java
        • Java version Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
        • All Android licenses accepted.
    
    [!] Android Studio (not installed)
        • Android Studio not found; download from https://developer.android.com/studio/index.html
          (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
    
    

    (I have installed android studio from IDEA toolbox though.)

    and from the shell I can run both commands:

    forest@forest-PC:~$ echo $JAVA_HOME
    /home/forest/.jdks/jdk1.8.0_261
    forest@forest-PC:~$ java -version
    java version "1.8.0_261"
    Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
    Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
    forest@forest-PC:~$ 
    

    It's also in the profile file:

    $ cat /etc/profile
    # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
    # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
    
    if [ "${PS1-}" ]; then
      if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
        # The file bash.bashrc already sets the default PS1.
        # PS1='\h:\w\$ '
        if [ -f /etc/bash.bashrc ]; then
          . /etc/bash.bashrc
        fi
      else
        if [ "`id -u`" -eq 0 ]; then
          PS1='# '
        else
          PS1='$ '
        fi
      fi
    fi
    
    if [ -d /etc/profile.d ]; then
      for i in /etc/profile.d/*.sh; do
        if [ -r $i ]; then
          . $i
        fi
      done
      unset i
    fi
    
    export JAVA_HOME="/home/forest/.jdks/jdk1.8.0_261"
    export PATH=$JAVA_HOME/bin:$PATH
    
    

    And in IntelliJ IDEA its also set: enter image description here enter image description here

    With Android studio, the exact same problem is present.

    IntelliJ console env also has $JAVA_HOME set.

    forest@forest-PC:~/projects/fluttertanulas/myapp$ echo $JAVA_HOME
    /home/forest/.jdks/jdk1.8.0_261
    forest@forest-PC:~/projects/fluttertanulas/myapp$ echo $PATH
    /home/forest/.jdks/jdk1.8.0_261/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/forest/Android/Sdk/tools:/home/forest/Android/Sdk/platform-tools
    forest@forest-PC:~/projects/fluttertanulas/myapp$ 
    forest@forest-PC:~/projects/fluttertanulas/myapp$ java -version
    java version "1.8.0_261"
    Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
    Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)