用Java 8开发的程序可以在Java 7上运行吗? [英] Can program developed with Java 8 be run on Java 7?

查看:237
本文介绍了用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中有一些新的类和方法,它们不能与较旧的类和方法一起使用.

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天全站免登陆