Flutter / Dart无法找到java和$JAVA_HOME。

huangapple go评论103阅读模式
英文:

Flutter / Dart cannot find java and $JAVA_HOME

问题

以下是翻译好的内容:

我正在按照 flutter.dev 上的官方教程进行操作,但是当我在 IntelliJ IDEA 中点击“运行”时,出现以下错误:

在调试模式下启动 sdk gphone x86 arm 上的 lib/main.dart...
正在运行 Gradle 任务 'assembleDebug'...

错误:未设置 JAVA_HOME,并且在您的 PATH 中找不到 'java' 命令。

请在环境中设置 JAVA_HOME 变量,以匹配您的 Java 安装位置。

异常:Gradle 任务 assembleDebug 退出代码为 1

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

$ flutter doctor -v
[✓] Flutter(通道 stable,1.22.1,位于 Linux,区域设置 en_US.UTF-8)
    • Flutter 版本 1.22.1,位于 /home/forest/snap/flutter/common/flutter
    • 框架修订 f30b7f4db9(11 天前),2020-10-08 10:06:30 -0700
    • 引擎修订 75bef9f6c8
    • Dart 版本 2.10.1

[✓] Android 工具链 - 用于 Android 设备的开发(Android SDK 版本 30.0.2)
    • 位于 /home/forest/Android/Sdk 的 Android SDK
    • 平台 android-30,构建工具 30.0.2
    • ANDROID_HOME = /home/forest/Android/Sdk
    • Java 二进制文件位于:/home/forest/.jdks/jdk1.8.0_261/bin/java
    • Java 版本 Java(TM) SE Runtime Environment(构建 1.8.0_261-b12)
    • 所有 Android 许可均已被接受。

[!] Android Studio(未安装)
    • 未找到 Android Studio;从 https://developer.android.com/studio/index.html 下载
      (或访问 https://flutter.dev/docs/get-started/install/linux#android-setup 以获取详细说明)。

(我已经通过 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:Bourne shell(sh(1))的系统范围的.profile 文件
# 和 Bourne 兼容的 shell(bash(1),ksh(1),ash(1),...)。

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # 文件 bash.bashrc 已经设置了默认的 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)
英文:

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:
Flutter / Dart无法找到java和$JAVA_HOME。
Flutter / Dart无法找到java和$JAVA_HOME。

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)

答案1

得分: 4

因为我刚刚安装了从 javaandroid,再到 flutterdart 等等的所有内容,并且我刚刚设置了我的 .bashrc/etc/profile,所以我只需要重新启动系统,让它生效。可能与 IDEA 如何获取环境变量有关。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月19日 16:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64424071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定