73

I have installed IntelliJ and I need to import a Gradle project.

I have build the gradle project using command prompt with the gradlew build command.

At the IntelliJ welcome page, I have proceeded with proper instructions, and when I choose "Finish". I get the following error:

Invalid Gradle JDK configuration found. Open Gradle Settings JAVA_HOME ennvironment variable not set.

When I click on "Open Gradle Settings" it pop up with error of Not found with a path under IntelliJ directory in Program files and searching in jre/jre/bin/....etc.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Sanuj
  • 1,077
  • 1
  • 11
  • 23

15 Answers15

142

Deleting .gradle and .idea will likely solve the problem.

So:

  • Close the project
  • Go to the project dir and delete .gradle and .idea
  • Get back and re-open the project using the IDE

These two must be generated locally on your PC (Some content of .idea might be version controlled though) and not pulled from a remote or somewhere else (Also they should be in .gitignore).

In my case the reason was that these two folders were generated on another computer and I had opened a project with these two folders existing before.

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
  • solve for me. however, i needed to import the project rather than open – Miguel Tomás Mar 01 '21 at 16:52
  • Hey! Thanks in my case i have to delete those two folder before open project in Android Studio. – Pratik Tank Jun 08 '21 at 03:38
  • Creating an empty Gradle project did not fixed, This one worked for me. – Abdul Salam Jun 09 '21 at 08:43
  • Works for me. Note that I had Android Studio 3.6 and i did a Gradle SyncBuild there and it was working then some funky error stopped it talking about my IntelliJ was too old o something and I should try again after upgrading to Android Studio 4.2. So I did the upgrade reluctantly and then everytime i opened the folder it would give me this error. Doing what this answer said fixed it. Tho in 3.6 it was working fine with the Github/Remote .gradle and .idea folders – LFMekz Jul 07 '21 at 22:46
76

Just found the solution :

  • Create an empty Gradle project, then go to "Project Structure" and check the path to JDK (it should be valid, if it isn't you can add your own path).
  • Then build this empty project, wait and once done, close IntelliJ.
  • Relaunch it and try to import/open your Gradle project, now it should work.
Eliott
  • 840
  • 8
  • 10
  • Yes it seems that when you run IntelliJ for the first time and try to run directly the "import gradle project" wizard, it fails to help you configuring the SDK. But when you run the "new gradle project" wizard, SDK is configured smoothly. Then you can cancel the wizard and go back to the "import gradle project" wizard. – Renaud May 22 '16 at 12:51
  • 1
    I just went to New -> Gradle project. It showed a valid JDK and I closed out of the wizard at that point. I was then able to refresh my gradle project once it was done indexing. – bblincoe Dec 09 '16 at 17:29
  • This didn't work for me because my JDK was not set up correctly (it was pointing to an older build of Java 8 after I'd upgraded). @albert-vila-calvo's answer worked for me. – Rónán Ó Braonáin Dec 14 '17 at 01:32
  • This worked for me! Invalidate cache/restart was not working. After creating an empty gradle project and going back to my springboot project resolved the issue. I also checked to see 'File -> Settings' 's 'Gradle' option is set as 'Gradle (default)' instead of 'Intellij IDEA'. Before facing this issue, I had upgraded gradle wrapper via terminal command 'gradle wrapper --gradle-version 6.8.2' – u33how Feb 11 '21 at 09:36
  • 1
    What a massive waste of time :'( – Daniel Wilson Jun 30 '21 at 16:15
  • Hello Daniel! Sorry to have wasted 2 minutes of your time. When I wrote this answer (5 years ago) this was the only solution I found. Nowadays there may be quicker solutions (see bellow answers). – Eliott Jul 01 '21 at 14:31
  • @Eliott Thank you , that is very helpful, this answer still works for me in 2022 today. – luochen1990 Apr 28 '22 at 03:07
38

You don't need to create a new project to fix this. You can do it from the main window (Configure -> Project Defaults -> Project Structure):

enter image description here

Then, on SDKs, set the appropriate JDK home path:

enter image description here

If you are on a Mac, click on the button with 3 dots and select the folder /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home.

I've found this here:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000266650-invalid-gradle-jdk-configuration-found

Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
  • It's solved for me but bit a different, first steps... Open the project then open project structure... then follow this steps – Kaloglu Jun 28 '21 at 17:14
19

Mac OS X Solution:

I had the same issue and fixed it by setting the JAVA_HOME environment variable using the command:

launchctl setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/

Refer to this answer on how to set environment variables in Mac OS X: Setting environment variables in OS X?

Community
  • 1
  • 1
Pedro Madrid
  • 1,887
  • 1
  • 20
  • 32
  • 3
    This worked for me, cheers. Just had to restart for IntelliJ to pick up on the env var being there. – Adam Jan 04 '17 at 13:22
  • I must be the odd duck still using csh, but this worked for me on Mac OS X Sierra: `setenv \`JAVA_HOME /usr/libexec/java_home\``. NOTE: `java_home` is a _command_ so it is enclosed in backticks so it is evaluated. – Peter Gluck Jun 11 '17 at 22:37
10

Close the project Go to the project dir and delete

  • .gradle
  • .idea

Get back and re-open the project using the IDE

sadiq rashid
  • 468
  • 5
  • 8
4

I recently had the same problem while importing a Gradle project. The trick was the remove the quotes from the JAVA_HOME variable. So instead of "C:\Program Files\Java\jdk1.8.0_66" my path now contains only the plain path C:\Program Files\Java\jdk1.8.0_66.

Julian Sievers
  • 329
  • 4
  • 10
  • Removing quotes around the JAVA_HOME variable worked for me. Several other tools and utilities require the quotes due to the space in the middle, so I put the spaces back when done. Thanks! – Bryan Jul 26 '16 at 14:44
3

To add to the previous responses, if you want to prevent this problem when cloning a repository in Git, you can simply remove .idea/misc.xml from your .gitignore file. This contains information about the project jar. For example:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK" />
</project>
andrewgazelka
  • 1,746
  • 1
  • 16
  • 26
1

For my case, I just restart the IDE and it works. It automatically download Gradle to suit the project version.

MnTanProject
  • 33
  • 1
  • 9
0

I had the same problem on the fresh installed Windows OS. I did not have a JDK at all and forgot to check it invalid JDK configuration . By default, you can check the Project configuration. If it is empty NO_SDK_ProjectStructure try to download JDK from Oracle web site and configure your project structure

0

I have faced same problem for tomcat 9 with my project based on Gradle. You can easily rectify the problem by configuring the application.properties file with the following code.

location - src/main/resources/application.properties

server.port = 9090

spring.security.user.name= admin

spring.security.user.password= password
0

My issue was not addressed by the above solution, instead root cause was that I've imported settings from my old system and internal Intellij configuration was invalid because first jdk that it had in the list in jdk.table.xml pointed to a wrong path. To fix this you should find this file in the intellij config folder and then simply open it with an editor and remove whole block related to the bad jdk version.

fury
  • 95
  • 1
  • 7
0

Close the project you are working on and then create another new project and build it and then close it and go back to your old project and it will work.

TT.
  • 15,774
  • 6
  • 47
  • 88
0

Comment this code on gradle.properties, the Issue was gone.

#org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 12 '22 at 12:23
0

I was getting:

Invalid JDK: /home/sz97/idea-IC-223.7571.182/jbr/bin/jlink is not a file! 
Ensure JAVA_HOME or buildSettings.javaHome is set to JDK 15 or newer
  • As mentioned by Mahdi-Malv, delete .gradle & .idea folders from the project directory.
  • Then delete other SDKs from File > Project Structure > Platform Settings > SDKs keeping the only required one.
  • Finally change the SDK version from Project Structure > Project Settings > Project.

This may solve the problem.

philoopher97
  • 772
  • 1
  • 6
  • 18
0

For me I was getting "Failed to resolve SDK" and "Try to find sdk" when I tried to run an application.

I looked in "File > Project Structure... > {Project, SDKs}"

I was looking at the list of SDKs in each. The entry was a red herring. I finally realised there was a "1.8" (no java version) entry in the list of SDKs in the Project dropdown. There was a second 1.8 (adopt ...) as well.

Answer => I fixed my issue by selecting the (1.8 java version "1.8...") entry. (Hence unselecting the currently selected "1.8" entry)

When I went back to the list the "1.8" entry magically disappeared.

After some digging I found it was was due to homebrew upgrading 1.8 but IntelliJ was using the old path.

PS Using IntelliJ (IntelliJ IDEA 2022.3.3 (Community Edition), Build #IC-223.8836.41, built on March 10, 2023)

Nick
  • 328
  • 2
  • 10