82

I started using Gradle and Intellij but I am having problems to configure Gradle's JVM. When I start a new Gradle project I am not allowed to define JVM as my JAVA_HOME variable. The following screenshots show what is happening:

new gradle project

As you can see Intellij says that my JAVA_HOME variable is not defined, however if I type echo $JAVA_HOME I can get my Java directory, in my case: /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

My ./~bash_profile is configured as follow: export JAVA_HOME=$(/usr/libexec/java_home)

Someone can figure what is happening ? Thank you!

ThomasR
  • 1,067
  • 1
  • 11
  • 16
user2568889
  • 907
  • 1
  • 7
  • 8
  • Do you have Java configured as one of your SDKs in IntelliJ? You don't explicitly require that JAVA_HOME be set if you do. – Makoto Jun 21 '15 at 00:21

7 Answers7

101

Bit counter-intuitive, but you must first setup a SDK for Java projects. On the bottom right of the IntelliJ welcome screen, select 'Configure > Project Defaults > Project Structure'.

The Project tab on the left will show that you have no SDK selected:

Therefore, you must click the 'New...' button on the right hand side of the dropdown and point it to your JDK. After that, you can go back to the import screen and it should be populated with your JAVA_HOME variable, providing you have this set.

RMcGuigan
  • 1,147
  • 2
  • 7
  • 11
  • 2
    Re: On the bottom right of the IntelliJ welcome screen - Can I get to the "welcome screen?" from the middle of a project? – RichMeister Oct 22 '15 at 19:45
  • 3
    @RichMeister "Project Structure" is also in the `File` menu with the shortcut `⌘-;` – Filippo Vitale May 02 '16 at 20:52
  • 2
    Hope some IntelliJ Developers are watching this thread and offer some decent error messages to avoid pulling hairs for this obscure setting? – Sid Oct 11 '16 at 19:42
64

The problem is your "Project SDK" is none! Add a "Project SDK" by clicking "New ..." and choose the path of JDK. And then it should be OK.

Harry.Chen
  • 1,582
  • 11
  • 12
  • Thanks. Can't seem to find any documentation on this. – honyovk Sep 09 '15 at 15:37
  • @honyovk Yes its really strange, I've spend around an hour trying to do this as my JAVA_HOME was set correctly and I removed the old version... Thanks again Harry.Chen – A Star Feb 24 '16 at 12:14
  • This is really strange actually. I followed your steps, clicked on the next button and on the `GroupId, ArtifactId and Version` screen I clicked on the cancel button. Backed to the first IDE screen I clicked open and the Gradle JVM was properly configured. – Filipe Brito Oct 16 '17 at 21:34
20

So far, nobody has answered the actual question.

Someone can figure what is happening ?

The problem here is that while the value of your $JAVA_HOME is correct, you defined it in the wrong place.

  • When you open a terminal and launch a Bash session, it will read the ~/.bash_profile file. Thus, when you enter echo $JAVA_HOME, it will return the value that has been set there.
  • When you launch IntelliJ directly, it will not read ~/.bash_profile … why should it? So to IntelliJ, this variable is not set.

There are two possible solutions to this:

  • Launch IntelliJ from a Bash session: open a terminal and run "/Applications/IntelliJ IDEA.app/Contents/MacOS/idea". The idea process will inherit any environment variables of Bash that have been exported. (Since you did export JAVA_HOME=…, it works!), or, the sophisticated way:
  • Set global environment variables that apply to all programs, not only Bash sessions. This is more complicated than you might think, and is explained here and here, for example. What you should do is run

    /bin/launchctl setenv JAVA_HOME $(/usr/libexec/java_home)
    

    However, this gets reset after a reboot. To make sure this gets run on every boot, execute

    cat << EOF > ~/Library/LaunchAgents/setenv.JAVA_HOME.plist
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
      <plist version="1.0">
      <dict>
        <key>Label</key>
        <string>setenv.JAVA_HOME</string>
        <key>ProgramArguments</key>
        <array>
          <string>/bin/launchctl</string>
          <string>setenv</string>
          <string>JAVA_HOME</string>
          <string>$(/usr/libexec/java_home)</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>ServiceIPC</key>
        <false/>
      </dict>
    </plist>
    EOF
    

    Note that this also affects the Terminal process, so there is no need to put anything in your ~/.bash_profile.

ThomasR
  • 1,067
  • 1
  • 11
  • 16
  • Note that this approach, like all of the approaches mentioned so far, other than launching from the command-line, will not dynamically update when Java is upgraded. The $(/usr/libexec/java_home) bit is evaluated only once when the file is created. – Stephen Rudolph Jan 22 '18 at 18:24
11

If you'd like to have your JAVA_HOME recognised by intellij, you can do one of these:

  • Start your intellij from terminal /Applications/IntelliJ IDEA 14.app/Contents/MacOS (this will pick your bash env variables)
  • Add login env variable by executing: launchctl setenv JAVA_HOME "/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home"

To directly answer your question, you can add launchctl line in your ~/.bash_profile

As others have answered you can ignore JAVA_HOME by setting up SDK in project structure.

gk0
  • 491
  • 5
  • 4
7

In my case I needed a lower JRE, so I had to tell IntelliJ to use a different one in "Platform Settings"

  • Platform Settings > SDKs ( +; )
  • Click the + button to add a new SDK (or rename and load an existing one)
  • Choose the /Contents/Home directory from the appropriate SDK
    (i.e. /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home)
Shanimal
  • 11,517
  • 7
  • 63
  • 76
2

Right Click On Project -> Open Module Settings -> Click SDK's

enter image description here

Choose Java Home Directory

Meghana Randad
  • 430
  • 5
  • 13
1

In my case helped to uninstall Android plugin - Idea kept bugging me to set Android NDK and did not let me change Java default SDK - and since I don't develop for Android I could remove it.

Ascorti
  • 41
  • 1
  • 4