用Java 8编写的程序可以在Java 7上运行吗? [英] A program made with Java 8 can be run on Java 7?

查看:439
本文介绍了用Java 8编写的程序可以在Java 7上运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑。


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

  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)?

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

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?


推荐答案

一般来说,没有。

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

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

有几个原因:


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

  • 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.

新JRE中的新类和方法不适用于较旧的JRE。

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

如果你真的真的想要(提示:你没有),你可以强制编译器将源视为Java的一个版本,并为另一个版本发出字节码,使用类似这样的东西:

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:

javac -source 1.8 -target 1.7 MyClass.java

对于Maven ),并针对JDK7进行编译,但在实践中,它通常不会比工作更有效。 我建议您不要。

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

编辑:JDK 8显然不支持这种确切的组合,所以这不行。其他一些版本组合可以正常工作。

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

还有一些程序可以将较新的Java程序转换为适用于较旧的JVM。要将Java 8转换为5-7,您可以尝试 https://github.com/orfjackal/retrolambda 以获得低于5 ,你可以选择其中一个: http://en.wikipedia.org/wiki/Java_backporting_tools

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/orfjackal/retrolambda To get lower than 5, you can pick one of these: http://en.wikipedia.org/wiki/Java_backporting_tools

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

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.

或者,既然你想在应用服务器上运行Java 8 JEE应用程序,那么只需在Java 8上运行整个服务器,它可能会奏效。

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编写的程序可以在Java 7上运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆