如果我用最新的 JDK 编译了一个 Java 文件,那么旧的 JVM 是否能够运行 .class 文件?

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

If I compiled a Java file with the newest JDK, would an older JVM be able to run the .class files?

javajvm

提问by m4design

Does the bytecode depend on the version of Java it was created with?

字节码是否取决于创建它的 Java 版本?

回答by Stephen C

If I compiled a java file in the newest JDK, would an older JVM be able to run the .class files?

如果我在最新的 JDK 中编译了一个 java 文件,那么旧的 JVM 是否能够运行 .class 文件?

That depends on three things:

这取决于三件事:

  • The actual Java versions you are talking about. For instance, a 1.4.0 JVM can run code compiled by a 1.4.2 compiler, but a 1.3.x JVM cannot1.

  • The compilation flags used. There is a -targetcompiler flag that tells it to generate code that will run on an older (target) JVM. And the -sourcecompiler flag tells it to only accept the older JVM's language features. (This approach won't always work, depending on the Java language features used by your code. But if the code compiles it should work.)

  • The library classes that the class file uses. If it uses library classes that don't exist in the older class libraries, then it won't run ... unless you can include a JAR that back-ports the classes2. You can avoid this problem by using the -bootclasspathoption to compile your code against the APIs of the older version of Java.

  • 您正在谈论的实际 Java 版本。例如,1.4.0 JVM 可以运行由 1.4.2 编译器编译的代码,但 1.3.x JVM 不能1

  • 使用的编译标志。有一个-target编译器标志告诉它生成将在旧(目标)JVM 上运行的代码。并且-source编译器标志告诉它只接受旧的 JVM 的语言特性。(这种方法并不总是有效,这取决于您的代码使用的 Java 语言功能。但如果代码编译,它应该可以工作。)

  • 类文件使用的库类。如果它使用旧类库中不存在的库类,则它不会运行……除非您可以包含一个向后移植类2的 JAR 。您可以通过使用-bootclasspath针对旧版本 Java 的 API 编译代码的选项来避免此问题。

Does the bytecode depend on the version of the java it was created with?

字节码是否取决于创建它的 Java 版本?

Yes, modulo the points above.

是的,以上述各点为模。



1 - The Java 8 JVMS states this: "Oracle's Java Virtual Machine implementation in JDK release 1.0.2supports class file format versions 45.0through 45.3inclusive. JDK releases 1.1.*support class file format versions in the range 45.0through 45.65535inclusive. For k ≥ 2, JDK release 1.ksupports class file format versions in the range 45.0through 44+k.0inclusive."

1 - Java 8 JVMS 声明:“JDK 版本中 Oracle 的 Java 虚拟机实现通过包含1.0.2支持类文件格式版本。JDK 版本支持通过包含范围内的类文件格式版本。对于 k ≥ 2,JDK 版本支持类文件格式通过包容性范围内的版本。”45.045.31.1.*45.045.655351.k45.044+k.0

2 - A backport could be problematic too. For example: 1) Things which depend on native code support would most likely require you to implementthat native code support. 2) You would most likely need to put any back-port JAR file onto the bootclasspath when you runthe code on the older JVM.

2 - 向后移植也可能有问题。例如:1) 依赖本机代码支持的事情很可能需要您实现本机代码支持。2) 当您在较旧的 JVM 上运行代码时,您很可能需要将任何后向端口 JAR 文件放入引导类路径。

回答by Andrew Thompson

Does the bytecode depend on the version of the java it was created with?

字节码是否取决于创建它的 Java 版本?

Normally yes. But by using the -source, -target and -bootclasspathoptions, a 1.7+ compiler can be used to create binaries that are compatible with Java 1.1

通常是的。但是通过使用 -source、-target和 -bootclasspath选项,可以使用 1.7+ 编译器创建与 Java 1.1 兼容的二进制文件

回答by MeBigFatGuy

First and foremost all java files have a version byte in the class header. Older jvms won't load classes with newer versions, regardless of what features they have.

首先,所有 java 文件在类头中都有一个版本字节。较旧的 jvm 不会加载较新版本的类,无论它们具有什么功能。

回答by wkl

JVM bytecode is forward compatible between major JVM version, but not backward compatible. However, for the best information you will have to read the JVM release notes because they typically indicate how backward compatible the bytecode is.

JVM 字节码在主要 JVM 版本之间向前兼容,但不向后兼容。但是,为了获得最佳信息,您必须阅读 JVM 发行说明,因为它们通常表明字节码向后兼容的程度。

Edit clarification since this caused discussion in the comments

编辑澄清,因为这引起了评论中的讨论

JVM bytecode is forward compatible, such that bytecode from one JVM is compatible with with later JVM releases. For example, you can take bytecode from the 1.4 JVM and run it in Java 5 or Java 6 JVM (aside from any sort of regression issues as pointed out by Andrew).

JVM 字节码是向前兼容的,这样来自一个 JVM 的字节码就可以与以后的 JVM 版本兼容。例如,您可以从 1.4 JVM 获取字节码并在 Java 5 或 Java 6 JVM 中运行它(除了 Andrew 指出的任何类型的回归问题)。

JVM bytecode is not backward compatible between JVMs, such that bytecode from a JVM is not guaranteed to work in a previous release of the JVM, as would be the case if you were attempting to run code compiled for Java 6 in a 1.4.2 JVM.

JVM 字节码在 JVM 之间不向后兼容,因此不能保证来自 JVM 的字节码在 JVM 的先前版本中工作,如果您尝试在 1.4.2 JVM 中运行为 Java 6 编译的代码,就会出现这种情况.

回答by Adeel Ansari

Does the bytecode depend on the version of the java it was created with?

字节码是否取决于创建它的 Java 版本?

Yes.

是的。

If I compiled a java file in the newest JDK, would an older JVM be able to run the .class files?

如果我在最新的 JDK 中编译了一个 java 文件,那么旧的 JVM 是否能够运行 .class 文件?

No. But the opposite will work, most likely. You might like see this interesting thread, it talks about backporting Java.

不,但相反的方法很可能会奏效。您可能喜欢看到这个有趣的线程,它讨论了向后移植 Java。

回答by OscarRyz

No, unless you specify as target the old JVM.

否,除非您将旧 JVM 指定为目标。

Eg.with Java 6 you can compile and run in Java 1.4 using:

例如,使用 Java 6,您可以使用以下命令在 Java 1.4 中编译和运行:

javac -target 1.4 SomeClass.java

Obviously the source code should be 1.4 compatible.

显然源代码应该是 1.4 兼容的。

回答by Hyman

You can compile classes that are older-version JVMs compatible if you don't use features available in higher JVMs.

如果您不使用更高版本 JVM 中可用的功能,您可以编译与旧版本 JVM 兼容的类。

javac -target 1.5 MyJava.java

javac -target 1.4 MyJava.java

javac -target 1.5 MyJava.java

javac -target 1.4 MyJava.java