user2568889
user2568889

Reputation: 997

Intellij JAVA_HOME variable

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!

Upvotes: 91

Views: 255176

Answers (8)

Kambaa
Kambaa

Reputation: 485

I'm having this problem too, after a fresh Linux mint 21.3 install using sdkman to setup jdk and maven and installing idea from toolbox app. on my OS terminal and idea's terminal window everything works fine, but using git tool window to commit returns problems(log says about not finding JAVA_HOME) on my projects. After finding this page https://ntsim.uk/posts/running-git-hooks-with-environment-variables-in-intellij and testing it, my issues are resolved. In summary i did

add this in the ~/.profile file:

export JAVA_HOME="$HOME/.sdkman/candidates/java/current"

after that i rebooted and checked if it worked. And it did! Have a nice day

Upvotes: 0

Ascorti
Ascorti

Reputation: 41

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.

Upvotes: 1

Meghana Randad
Meghana Randad

Reputation: 450

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

enter image description here

Choose Java Home Directory

Upvotes: 2

ThomasR
ThomasR

Reputation: 1135

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.

Upvotes: 27

Shanimal
Shanimal

Reputation: 11718

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)

Upvotes: 7

gk0
gk0

Reputation: 501

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.

Upvotes: 12

RMcGuigan
RMcGuigan

Reputation: 1147

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.

Upvotes: 101

Harry.Chen
Harry.Chen

Reputation: 1610

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.

Upvotes: 66

Related Questions