java 管理多个版本的JDK:无效的源发布错误

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

managing multiple versions of JDK: invalid source release error

java

提问by John

I have jdk 1.6 and 1.7 installed.

我安装了 jdk 1.6 和 1.7。

I have updated my JAVA_HOME variable to

我已将 JAVA_HOME 变量更新为

C:\Program Files\Java\jdk1.6.0_24;C:\Program Files\Java\jdk1.7.0

But when I try to run java code.

但是当我尝试运行 java 代码时。

>javac -version
javac 1.6.0_24

>java -version
java version "1.7.0_09"

>javac -source 1.7.0 -target 1.7.0 Test.java
javac: invalid source release: 1.7.0

回答by cutebug

Your jdk is still pointing to JDK 1.6. javac is bundled with jdk, while java -version ideally runs even if jre is installed properly.

您的 jdk 仍然指向 JDK 1.6。javac 与 jdk 捆绑在一起,而 java -version 即使正确安装了 jre 也能正常运行。

回答by Y?co

The simplest solution would be to use the java_home tool.

最简单的解决方案是使用 java_home 工具。

/usr/libexec/java_home -v 1.7.0_51 --exec javac -version

回答by Anders Petersson

Your "javac -version" test shows that you are using javac 1.6. Then you are trying to build for 1.7 which is a future version that is unknown to the 1.6 compiler. I would do as some comments suggests and only define one JDK for a given project.

您的“javac -version”测试表明您使用的是 javac 1.6。然后您正在尝试为 1.7 构建,这是 1.6 编译器未知的未来版本。我会按照一些评论的建议去做,并且只为给定的项目定义一个 JDK。

回答by semTex

What about javac -source 1.7 -target 1.7 Test.java?

怎么样javac -source 1.7 -target 1.7 Test.java