Flutter / Dart 找不到 java 和 $JAVA_HOME

Posted

技术标签:

【中文标题】Flutter / Dart 找不到 java 和 $JAVA_HOME【英文标题】:Flutter / Dart cannot find java and $JAVA_HOME 【发布时间】:2021-02-02 00:52:50 【问题描述】:

我正在关注official tutorial on flutter.dev,但在 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

如果我运行flutter doctor -v,您可以看到它已安装:

$ 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).

(不过,我已经从 IDEA 工具箱安装了 android studio。)

我可以在 shell 中运行这两个命令:

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:~$ 

它也在配置文件中:

$ 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

在 IntelliJ IDEA 中它还设置:

对于 Android Studio,存在完全相同的问题。

IntelliJ 控制台 env 也设置了 $JAVA_HOME。

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)

【问题讨论】:

【参考方案1】:

由于我刚刚安装了所有东西,从 javaandroidflutterdart 等,我只设置了我的 .bashrc/etc/profile,我只需要在系统上重新启动即可让它起作用。可能是关于 IDEA 如何获取环境变量的问题。

【讨论】:

不是 IDEA,而是您使用的系统。 .profile 在登录时加载,.bashrc 在您打开终端窗口时加载。 我的关键是重启。

以上是关于Flutter / Dart 找不到 java 和 $JAVA_HOME的主要内容,如果未能解决你的问题,请参考以下文章

Flutter / VSC 在你的 Flutter SDK 中找不到 Dart

在 Flutter SDK 中找不到 Dart

在 Intellij IDEA 中创建新的 dart 项目时,在 dart 模板下找不到 Flutter Web 应用程序选项

Flutter 错误:找不到 Getter:english_words.dart 包中的“wordPair”

Dart/flutter 小部件测试,无论是按键还是文本都找不到文本小部件

在 Flutter 项目中找不到 layout.xml