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 variabl
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.
If you'd like to have your JAVA_HOME recognised by intellij, you can do one of these:
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.
In my case I needed a lower JRE, so I had to tell IntelliJ to use a different one in "Platform Settings"
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.
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.
~/.bash_profile
file. Thus, when you enter echo $JAVA_HOME
, it will return the value that has been set there.~/.bash_profile
… why should it? So to IntelliJ, this variable is not set.There are two possible solutions to this:
"/Applications/IntelliJ IDEA.app/Contents/MacOS/idea"
. The idea
process will inherit any environment variables of Bash that have been export
ed. (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
.