Android Studio 找不到 JAVA_HOME

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22001328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:57:39  来源:igfitidea点击:

Android Studio can't find JAVA_HOME

javaandroidandroid-studio

提问by Naftuli Kay

I can't seem to get my Android applications to compile due to a really annoying exception from Android Studio:

由于来自 Android Studio 的一个非常烦人的异常,我似乎无法编译我的 Android 应用程序:

]()![enter image description here

]()![在此处输入图片描述

Execution failed for task ':myapp-services:compileDebugJava'.
    Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.

I'm on Ubuntu 12.04, and I'm running Oracle's Java 7 JRE/JDK.

我在 Ubuntu 12.04 上,我正在运行 Oracle 的 Java 7 JRE/JDK。

In ~/.bashrc:

~/.bashrc

export JAVA_HOME="/usr/lib/jvm/java-7-oracle/"

In android-studio/bin/studio.sh:

android-studio/bin/studio.sh

export JAVA_HOME="/usr/lib/jvm/java-7-oracle/"

Output of javac -version:

的输出javac -version

javac 1.7.0_51

Output of java -version:

的输出java -version

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

Output of printenv JAVA_HOME:

的输出printenv JAVA_HOME

/usr/lib/jvm/java-7-oracle

Output of which javac:

的输出which javac

/usr/bin/javac

I have also logged out and logged back in again to my session.

我也已注销并重新登录到我的会话。

I'm really not sure what I'm missing here. Why can't Android Studio compile my application?

我真的不确定我在这里错过了什么。为什么 Android Studio 不能编译我的应用程序?

采纳答案by Naftuli Kay

I found a solution in a different answer:

我在不同的答案中找到了解决方案:

rm ~/.AndroidStudioPreview/config/options/jdk.table.xml

What seems to have happened is that something was configured for a previous version of Android Studio and this configuration lived too long :)

似乎已经发生的事情是为以前版本的 Android Studio 配置了一些东西,而这个配置存在的时间太长了 :)

回答by P. Lalonde

Just downloaded the latest version (android-studio-ide-1641136.dmg) and I just changed the Info.plist file content. It's originally configured to use 1.6* as of Java version. I have 1.8 installed, so just changed to 1.8* and worked.

刚刚下载了最新版本(android-studio-ide-1641136.dmg),我刚刚更改了 Info.plist 文件内容。从 Java 版本开始,它最初配置为使用 1.6*。我安装了 1.8,所以只是更改为 1.8* 并工作。

I am on Mac Yosemite.

我在 Mac 优胜美地。

回答by FalconBot

I have found a problem with the Android Studio studio.bat file and here it is

我发现 Android Studio studio.bat 文件有问题,这里是

    ::------------------------------------------------------
    :: Locate a JDK installation directory which will be used to run the IDE.
    :: Try (in order): ANDROID_STUDIO_JDK, ..\jre, JDK_HOME, JAVA_HOME.
    :: ---------------------------------------------------------------------
    IF EXIST "%ANDROID_STUDIO_JDK%" SET JDK=%ANDROID_STUDIO_JDK%
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%~dp0\..\jre" SET JDK=%~dp0\..\jre
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%JDK_HOME%" SET JDK=%JDK_HOME%
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME%
    IF "%JDK%" == "" GOTO error

Do you get the problem in the last 2 lines of code?
It seems that if your environmental variable is %JAVA_HOME% the batch file will GOTO error and an error message will be displayed and that's it NO ANDROID STUDIO FOR YOU, even though %JAVA_HOME% contains a valid path but if the environmental variable is something like %ANDROID_STUDIO_JDK% like mine is because I created it myself then it will work

您是否在最后两行代码中遇到问题?
看来,如果您的环境变量是 %JAVA_HOME% 批处理文件将转到错误并显示一条错误消息,就是这样 NO ANDROID STUDIO FOR YOU,即使 %JAVA_HOME% 包含有效路径但如果环境变量类似于%ANDROID_STUDIO_JDK% 像我这样是因为我自己创建了它然后它会工作



PROPOSED SOLUTIONS

建议的解决方案

(1) Edit the batch file Where you see this IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME% IF "%JDK%" == "" GOTO error
Change it to this
IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME% IF NOT "%JDK%" == "" GOTO jdk IF "%JDK%" == "" GOTO error
(2) Create an environmental variable named %ANDROID_STUDIO_JDK% as shown here https://kb.wisc.edu/cae/page.php?id=24500

(1) 编辑批处理文件在你看到这个的地方把 IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME% IF "%JDK%" == "" GOTO error

IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME% IF NOT "%JDK%" == "" GOTO jdk IF "%JDK%" == "" GOTO error
改成这个(2) 创建一个名为 %ANDROID_STUDIO_JDK% 的环境变量,如下所示 https://kb.wisc.edu/cae/page.php?id=24500

回答by Loubake

I was getting this error too. After many hours trying to solve it, I've managed to solve the problem.

我也收到这个错误。经过几个小时的尝试解决它,我设法解决了这个问题。

In my case, executing the app from command line did the trick!

就我而言,从命令行执行应用程序就成功了!

Here's what I did (I'm using Windows 10). Please, run these commands on the root of the project (where we have the appdirectory):

这就是我所做的(我使用的是 Windows 10)。请在项目的根目录(我们有app目录)运行这些命令:

1) gradlew assembleDebug

1) gradlew assembleDebug

  • Gradle will execute its tasks and you should see the message: BUILD SUCCESSFUL
  • Gradle 将执行其任务,您应该会看到以下消息:BUILD SUCCESSFUL

2) adb install -r app\build\outputs\apk\app-debug-unaligned.apk

2) adb install -r app\build\outputs\apk\app-debug-unaligned.apk

  • After executing this line, you'll see the message: [100%] /data/local/tmp/app-debug-unaligned.apk pkg: /data/local/tmp/app-debug-unaligned.apk Success
  • 执行此行后,您将看到消息: [100%] /data/local/tmp/app-debug-unaligned.apk pkg: /data/local/tmp/app-debug-unaligned.apk Success

3) adb shell am start -n package_name/package_name.MainActivity, where "package_name" must be replaced by your package name (you can find it on AndroidManifest.xml). If "MainActivity" is not your launcher activity, replace it by your own activity.

3) adb shell am start -n package_name/package_name.MainActivity,其中“package_name”必须替换为您的包名(您可以在 AndroidManifest.xml 上找到它)。如果“MainActivity”不是您的启动器活动,请将其替换为您自己的活动。

  • You'll see the message: Starting: Intent { cmp=package_name/.MainActivity }, where "package_name" is your app' package name.
  • 您将看到以下消息:Starting: Intent { cmp=package_name/.MainActivity },其中“package_name”是您的应用程序包名称。

It will install the app on your device and after that, Android Studio seems to find the JAVA_HOME variable again. You should be able to run the app by hitting the "Run" button on Android Studio.

它将在您的设备上安装该应用程序,之后,Android Studio 似乎再次找到 JAVA_HOME 变量。您应该能够通过点击 Android Studio 上的“运行”按钮来运行该应用程序。

Hope it helps someone!

希望它可以帮助某人!

回答by user3024215

Check echo $JAVA_HOME

检查回声 $JAVA_HOME

if it doesn't gives you correct path. follow setting JAVA_HOME path on Ubuntu

如果它没有给你正确的路径。按照在 Ubuntu 上设置 JAVA_HOME 路径

if it gives you correct path set then you need to update your gradle plugin version (It worked for me) and rebuild project.

如果它为您提供了正确的路径集,那么您需要更新您的 gradle 插件版本(它对我有用)并重建项目。

回答by Don Chakkappan

I solved it by updating build.gradle (Top-Level) with

我通过更新 build.gradle (Top-Level) 解决了这个问题

classpath 'com.android.tools.build:gradle:2.1.2'

回答by Ankit Saini

I have solved it by updating classpath of (build.gradle (Project:projectName))

我已经通过更新 (build.gradle (Project:projectName)) 的类路径解决了它

classpath 'com.android.tools.build:gradle:2.2.3' 

回答by Keshav Gera

update build.gradle module

更新 build.gradle 模块

    classpath 'com.android.tools.build:gradle:3.0.1'

Solved my issue

解决了我的问题

回答by Kiran Maniya

Go To Settings->Project Structure->JDK path and change it to the system JDK instead of Studio JDK, you will be up and running.

转到 Settings->Project Structure->JDK path 并将其更改为系统 JDK 而不是 Studio JDK,您将启动并运行。