Java 针对不同版本的 JRE

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

Target different version of JRE

javacompilationversionjavac

提问by jww

I'm testing Java on a Windows Surface Pro. The Surface Pro has Java 7 Update 55, but does nothave the JDK installed.

我正在 Windows Surface Pro 上测试 Java。表面Pro拥有的Java 7更新55,但具备安装JDK。

I compiled a program on my MacBook from the command line using javac. The MacBook Has Java 8 Update 5 and it includes the JDK (obviously because I compiled on the MBP).

我使用javac. MacBook 具有 Java 8 Update 5 并且包含 JDK(显然是因为我是在 MBP 上编译的)。

When I move the program from the MackBook to the Surface Pro, I get the following error. Here, "moving the program" means copying the two *.classfiles to the new machine and trying to execute the byte codes.

当我将程序从 MacBook 移动到 Surface Pro 时,出现以下错误。这里,“移动程序”是指将两个*.class文件复制到新机器上,并尝试执行字节码。

java.lang.UnsupportedClassVersionError: HttpsTest : Unsupported major.minor version 52.

Trying to compile with target information results in:

尝试使用目标信息进行编译会导致:

$ javac -target 1.7 HttpsTest.java SSLSocketFactoryEx.java
javac: target release 1.7 conflicts with default source release 1.8

javac -helpdoes not list any options to control the version information of the compiled program (other than target, which does not appear to work).

javac -help没有列出任何选项来控制编译程序的版本信息(除了target,它似乎不起作用)。

How do I compile my source files for down level versions of a JRE? In this case, I'm targeting Java 7 from a Java 8 machine.

如何为 JRE 的下层版本编译源文件?在这种情况下,我的目标是来自 Java 8 机器的 Java 7。

采纳答案by Daniel Stolz

From the command line, change your call to:

从命令行,将您的呼叫更改为:

javac -source 1.7 -target 1.7 HttpsTest.java SSLSocketFactoryEx.java

The documentation for javac (for JDK 8) is here.

javac(用于 JDK 8)的文档在这里

The documentation for javac (for JDK 9) is here.

javac(用于 JDK 9)的文档在这里

Note: In JDK 9, -target is replaced with --release.

注意:在 JDK 9 中,-target 被替换为 --release。

回答by chux

If you are using eclipse you can set compiler compliance level in the project properties-> Java Compiler. So your code is compiled for the chosen Java version. See here:

如果您使用的是 Eclipse,您可以在项目属性-> Java 编译器中设置编译器合规性级别。因此,您的代码是针对所选 Java 版本编译的。看这里:

enter image description here

在此处输入图片说明

回答by Piyush Gharote

In the error message major.minor version 52.the number 52here represents the java version with which the class is compiled for example

在错误消息中major.minor version 52.,这里的数字52代表编译类的 Java 版本,例如

Java SE 9 = 53
Java SE 8 = 52
Java SE 7 = 51
Java SE 6.0 = 50
Java SE 5.0 = 49
JDK 1.4 = 48
JDK 1.3 = 47
JDK 1.2 = 46
JDK 1.1 = 45

so your installed JRE is at higher version than your target use -sourcewith the version number to which you want to compile in your case:

所以你安装的 JRE 的版本比你的目标-source版本高,你想在你的情况下编译到的版本号:

javac -source 1.7 -target 1.7 HttpsTest.java SSLSocketFactoryEx.java

javac -source 1.7 -target 1.7 HttpsTest.java SSLSocketFactoryEx.java