用 Java 8 开发的程序可以在 Java 7 上运行吗?

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

Can program developed with Java 8 be run on Java 7?

javabackwards-compatibilityjava-8java-server

提问by Mitsu Garcia

I am a little confused.

我有点困惑。

  1. Oracle says Java 8 is highly compatible with Java 7 (backward). But, what possibilities exist that Java 8 program can be run on Java 7 successfully (SE/EE)?

  2. If point one was true, Java 8 applications will be deployed and executed on a Java 7 server support? for example, Tomcat 8 or WildFly?

  1. Oracle 表示 Java 8 与 Java 7 高度兼容(向后)。但是,Java 8 程序可以在 Java 7 (SE/EE) 上成功运行的可能性有多大?

  2. 如果第一点是真的,Java 8 应用程序将在 Java 7 服务器支持上部署和执行吗?例如,Tomcat 8 或 WildFly?

采纳答案by Karol S

In general, no.

一般来说,没有。

The backwards compatibility means that you can run Java 7 program on Java 8 runtime, not the other way around.

向后兼容性意味着您可以在 Java 8 运行时上运行 Java 7 程序,而不是相反。

There are several reasons for that:

这有几个原因:

  • Bytecode is versioned and JVM checks if it supports the version it finds in .class files.

  • Some language constructs cannot be expressed in previous versions of bytecode.

  • There are new classes and methods in newer JRE's which won't work with older ones.

  • 字节码是版本化的,JVM 检查它是否支持它在 .class 文件中找到的版本。

  • 某些语言结构无法在以前版本的字节码中表达。

  • 较新的 JRE 中有新的类和方法,它们不适用于旧的。

If you really, really want (tip: you don't), you can force the compiler to treat the source as one version of Java and emit bytecode for another, using something like this:

如果您真的非常想要(提示:您不想要),您可以强制编译器将源代码视为 Java 的一个版本并为另一个版本发出字节码,使用如下所示:

javac -source 1.8 -target 1.7 MyClass.java

(the same for Maven), and compile against JDK7, but in practice it will more often not work than work. I recommend you don't.

Maven 相同),并针对 JDK7 进行编译,但在实践中,它通常不会工作而不是工作。我建议你不要。

EDIT: JDK 8 apparently doesn't support this exact combination, so this won't work. Some other combinations of versions do work.

编辑:JDK 8 显然不支持这种确切的组合,所以这行不通。一些其他版本的组合确实有效。

There are also programs to convert newer Java programs to work on older JVM's. For converting Java 8 to 5-7, you can try https://github.com/orfHymanal/retrolambdaTo get lower than 5, you can pick one of these: http://en.wikipedia.org/wiki/Java_backporting_tools

还有一些程序可以将较新的 Java 程序转换为在较旧的 JVM 上工作。要将Java 8 转换为 5-7,您可以尝试https://github.com/orfHymanal/retrolambda要低于 5,您可以选择以下之一:http: //en.wikipedia.org/wiki/Java_backporting_tools

None of these hacks will give you new Java 8 classes and methods, including functional programming support for collections, streams, time API, unsigned API, and so on. So I'd say it's not worth it.

这些 hack 都不会为您提供新的 Java 8 类和方法,包括对集合、流、时间 API、未签名 API 等的函数式编程支持。所以我会说这不值得。

Or, since you want to run your Java 8 JEE applications on an application server, just run your entire server on Java 8, it may work.

或者,由于您想在应用程序服务器上运行 Java 8 JEE 应用程序,只需在 Java 8 上运行整个服务器,它可能会工作。

回答by Rahul Tripathi

No backward compatibility means that Java7 programs will run under Java8 but the reverse is not alwaystrue

没有向后兼容性意味着 Java7 程序将在 Java8 下运行,但反过来并不总是如此

You may also check Oracle Limit Backward Compatibility

您还可以检查Oracle Limit Backward Compatibility

回答by JavaLearner

Backward compatibility means

向后兼容意味着

You can Run Lower configuration on Higher Configuration not Vice-Versa.

您可以在较高配置上运行较低配置而不是Vice-Versa

回答by ctutte

In general, new versions have to give backwards compatibility, so people dont have to throw their work and can upgrade easily. The other way round (newer version running in older version) is not necesarily true because if you use some new implemented feature, that feature obviously does not exist in the previous version and won't work.

一般来说,新版本必须提供向后兼容性,因此人们不必放弃他们的工作并且可以轻松升级。反过来(在旧版本中运行的新版本)不一定正确,因为如果您使用某些新实现的功能,则该功能显然在以前的版本中不存在并且无法使用。

Regards

问候

回答by Nate

Well, there is the -targetcompiler option, which lets you target the class file format of previous java versions. However, this doesn't fix or detect things such as using classes or methods introduced in JDK APIs after the target version.

嗯,有-target编译器选项,它可以让您定位以前 Java 版本的类文件格式。但是,这不会修复或检测诸如使用目标版本之后 JDK API 中引入的类或方法之类的问题。

回答by Sandeep Shankar Harikrishnan

I generated stubs from WSDL, compiled in java 8 and was able to deploy them on server having java 1.6 jvm on it.

我从 WSDL 生成存根,在 java 8 中编译,并且能够将它们部署在具有 java 1.6 jvm 的服务器上。