Java IntelliJ IDEA 给出“所选目录不是 JDK 的有效主目录”错误,即使它是有效的 JDK

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51344643/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:05:33  来源:igfitidea点击:

IntelliJ IDEA gives “The selected directory is not a valid home for JDK” error even it is a valid JDK

javaintellij-idea

提问by amone

I'm trying to set a JDK path and IntelliJ gives this error:

我正在尝试设置 JDK 路径,而 IntelliJ 给出了这个错误:

The selected directory is not a valid home for JDK

The selected directory is not a valid home for JDK

But I am sure that it is absolutely valid because I can use the java compiler.

但我确信它是绝对有效的,因为我可以使用 java 编译器。

javac Main.javacompiles the file correctly.

javac Main.java正确编译文件。

I am sure that the path is also correct because the command below gives me that path.

我确信路径也是正确的,因为下面的命令给了我那个路径。

/usr/libexec/java_homeprints the path below:

/usr/libexec/java_home打印以下路径:

/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home

When I choose this path on IntelliJ, it shows the error. I reinstalled the JDK and also tried to choose /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/bin/but none of them worked.

当我在 IntelliJ 上选择此路径时,它显示错误。我重新安装了 JDK 并尝试选择/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/bin/但它们都不起作用。

Please do not consider this question is a duplicate because I tried all of the solutions offered in other answers.

请不要认为这个问题是重复的,因为我尝试了其他答案中提供的所有解决方案。

采纳答案by dossani

For the latest version of Intellij community edition, check their release notes here : https://confluence.jetbrains.com/display/IDEADEV/IntelliJ+IDEA+2018.1+181.5540.7+Release+Notes

有关 Intellij 社区版的最新版本,请在此处查看其发行说明:https://confluence.jetbrains.com/display/IDEADEV/IntelliJ+IDEA+2018.1+181.5540.7+Release+Notes

Seems there's a bug "Manually provided JDK 10 not accepted in JShell: "JDK version is 8"

似乎有一个错误“JShell 不接受手动提供的 JDK 10:”JDK 版本为 8”

Hope it helps.

希望能帮助到你。

Thanks.

谢谢。

回答by Codex

I think you only need to set the jdk path folder and not to the bin itself in Intellij. I dont know if that is what you're asking.

我认为您只需要在 Intellij 中设置 jdk 路径文件夹,而不是设置到 bin 本身。不知道你问的是不是这个。

回答by Ionu? Ciuta

I had the same issue, but - in my case and I'm pretty confident in your case too - it is not related to the bug presented in the accepted answer. For some reason, IDEA IntelliJ doesn't accept any OpenJDKs. I tried all version from 7 to 9. It worked immediately once I installed the OracleJDK (version 10 this time).

我有同样的问题,但是 - 就我而言,我对你的情况也非常有信心 - 它与接受的答案中提出的错误无关。出于某种原因,IDEA IntelliJ 不接受任何 OpenJDK。我尝试了从 7 到 9 的所有版本。一旦我安装了OracleJDK(这次是版本 10),它就立即起作用了。

Also, you might have multiple JDK installed and when running the java command in the terminal - without knowing - you may be actually using a different JDK than the one you think you're using.

此外,您可能安装了多个 JDK,并且在终端中运行 java 命令时 - 在不知道的情况下 - 您实际上使用的 JDK 可能与您认为正在使用的 JDK 不同。

Use which javaand then ls -lthe resulted path to see what's behind the symlink.

使用which java,然后ls -l使用结果路径查看符号链接背后的内容。

回答by Thilan

That is because of tool.jar and/or rt.jar file(s) being missed from JAVA_HOME/lib directory.

这是因为 JAVA_HOME/lib 目录中缺少 tool.jar 和/或 rt.jar 文件。

Reinstalling java can resolve this issue.

重新安装java可以解决这个问题。

回答by hpu

Make sure your version of IntelliJ is newer then the JDK! (or better go to the newest version of IntelliJ)

确保您的 IntelliJ 版本比 JDK 新!(或者最好去最新版本的 IntelliJ)