Intellij JAVA_HOME 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30960103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intellij JAVA_HOME variable
提问by user2568889
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:
我开始使用 Gradle 和 Intellij,但在配置 Gradle 的 JVM 时遇到问题。当我开始一个新的 Gradle 项目时,我不允许将 JVM 定义为我的 JAVA_HOME 变量。以下屏幕截图显示了正在发生的事情:
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
正如你所看到的,Intellij 说我的 JAVA_HOME 变量没有定义,但是如果我输入echo $JAVA_HOME
我可以得到我的 Java 目录,在我的例子中:/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
My ./~bash_profile is configured as follow:
export JAVA_HOME=$(/usr/libexec/java_home)
我的 ./~bash_profile 配置如下:
export JAVA_HOME=$(/usr/libexec/java_home)
Someone can figure what is happening ? Thank you!
有人可以弄清楚发生了什么?谢谢!
采纳答案by Harry.Chen
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.
问题是你的“项目 SDK”没有!通过单击“新建...”添加“项目 SDK”并选择 JDK 的路径。然后应该就OK了。
回答by RMcGuigan
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'.
有点违反直觉,但您必须首先为 Java 项目设置一个 SDK。在 IntelliJ 欢迎屏幕的右下角,选择“配置 > 项目默认值 > 项目结构”。
The Project tab on the left will show that you have no SDK selected:
左侧的 Project 选项卡将显示您没有选择 SDK:
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.
因此,您必须单击下拉菜单右侧的“新建...”按钮并将其指向您的 JDK。之后,您可以返回到导入屏幕,它应该填充您的 JAVA_HOME 变量,前提是您有这个设置。
回答by gk0
If you'd like to have your JAVA_HOME recognised by intellij, you can do one of these:
如果您想让 intellij 识别您的 JAVA_HOME,您可以执行以下操作之一:
- 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"
- 从终端 /Applications/IntelliJ IDEA 14.app/Contents/MacOS 启动您的智能(这将选择您的 bash 环境变量)
- 通过执行添加登录环境变量:
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
要直接回答您的问题,您可以在 ~/.bash_profile 中添加 launchctl 行
As others have answered you can ignore JAVA_HOME by setting up SDK in project structure.
正如其他人所回答的那样,您可以通过在项目结构中设置 SDK 来忽略 JAVA_HOME。
回答by Shanimal
In my case I needed a lower JRE, so I had to tell IntelliJ to use a different one in "Platform Settings"
在我的情况下,我需要一个较低的 JRE,所以我不得不告诉 IntelliJ 在“平台设置”中使用不同的
- 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)
- 平台设置 > SDK ( ⌘+ ;)
- 单击+按钮添加新的 SDK(或重命名并加载现有的 SDK)
- 从相应的 SDK 中选择 /Contents/Home 目录
(即 /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home)
回答by ThomasR
So far, nobody has answered the actual question.
到目前为止,没有人回答实际问题。
Someone can figure what is happening ?
有人可以弄清楚发生了什么?
The problem here is that while the valueof your $JAVA_HOME
is correct, you defined it in the wrong place.
这里的问题是,虽然你的值$JAVA_HOME
是正确的,但你在错误的地方定义了它。
- When you open a terminal and launch a Bash session, it will read the
~/.bash_profile
file. Thus, when you enterecho $JAVA_HOME
, it will return the value that has been set there. - When you launch IntelliJ directly, it will notread
~/.bash_profile
… why should it? So to IntelliJ, this variable is not set.
- 当您打开终端并启动 Bash 会话时,它将读取该
~/.bash_profile
文件。因此,当您输入 时echo $JAVA_HOME
,它将返回已在那里设置的值。 - 当你直接启动 IntelliJ 时,它不会读取
~/.bash_profile
……为什么要这样?所以对于 IntelliJ,这个变量没有设置。
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"
. Theidea
process will inherit any environment variables of Bash that have beenexport
ed. (Since you didexport 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 hereand 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
.
- 从 Bash 会话启动 IntelliJ:打开终端并运行
"/Applications/IntelliJ IDEA.app/Contents/MacOS/idea"
. 该idea
进程将继承任何已export
编辑过的 Bash 环境变量。(因为你做了export JAVA_HOME=…
,它有效!),或者,复杂的方式: 设置适用于所有程序的全局环境变量,而不仅仅是 Bash 会话。这比您想象的要复杂,例如,在此处和此处进行了解释。你应该做的是运行
/bin/launchctl setenv JAVA_HOME $(/usr/libexec/java_home)
但是,这会在重新启动后重置。为了确保它在每次启动时运行,执行
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
请注意,这也会影响终端进程,因此无需在
~/.bash_profile
.