Solving “Could not determine Java version” with Gradle and IntelliJ on MacOSX

A couple of times in the recent past I’ve been exposed to the “Could not determine Java version” error message when opening a new Gradle project from IntelliJ. This happens when I navigate to File -> Open and select the build.gradle file for the new project. It doesn’t matter if I choose to import the project using the default Gradle wrapper, a local distribution or something else. Restarting IntelliJ and invalidating its caches didn’t work and looking at the IntelliJ log files didn’t gave me many clues. So what I will present next are two different approaches I’ve taken to get around the problem that worked for me on different occasions. Hopefully this can help someone else in the future.

Solution 1 – Check your SDKs

If you’ve upgraded a JDK recently make sure to check that the SDK paths are correct. It doesn’t have to be the SDK you’re currently using your project, it can be another version. To do this on MacOSX press F4 at the project to go to its project structure settings. Here click on SDKs and make sure that each Java JDK points to a valid folder. For example in my case the JDK 6 SDK pointed to a folder that no longer existed and once I updated that I could import the Gradle project. It’s of course also possible to delete a JDK from IntelliJ if it has been removed from disk.

Solution 2 – Change IntelliJ JDK

On a different occasion I ran into the same problem again but this time my JDKs all seemed to be in order so I couldn’t figure out what was going on. After a good deal of trail and error I tried changing the JDK version used to start IntelliJ itself from JDK 6 to JDK 8 and this resolved the problem. To do this you need to edit the Info.plist located at /Applications/<IntelliJ Version>.app/Contents/Info.plist. Scroll down to the section that says <key>JVMVersion</key> and change its value to 1.8*. For example:

Restarting IntelliJ and reimporting the Gradle project solved it for me.

Conclusion

I’ve presented two different workarounds here but I hope that JetBrains will be able to improve the situation in the future (which I’m sure they will). Until then I hope that someone in the same situation like me will have some help from this blog.

2 Comments

  1. Thanks a lot! Solution #2 did the trick for me on Ubuntu, using openjdk.

  2. Connor Garvey

    Thank you! I couldn’t find that file in Ubuntu, but wrapping the IntelliJ script in this one solved the problem.

    #! /usr/bin/env sh
    IDEA_JDK=/usr/lib/jvm/java-1.8.0-openjdk-amd64 [intellij directory]/bin/idea.sh

Leave a Reply