Intellij JAVA_HOME variable

后端 未结 5 1517
南旧
南旧 2021-01-31 07:00

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

5条回答
  •  感情败类
    2021-01-31 07:54

    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
      
      
        
        
          Label
          setenv.JAVA_HOME
          ProgramArguments
          
            /bin/launchctl
            setenv
            JAVA_HOME
            $(/usr/libexec/java_home)
          
          RunAtLoad
          
          ServiceIPC
          
        
      
      EOF
      

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

提交回复
热议问题