简体   繁体   English

JRE 1.4是否支持使用Java 1.5和1.6编译的类?

[英]Will JRE 1.4 support classes compiled with Java 1.5 & 1.6?

Will code compiled using 1.5 and 1.6 run on a 1.4 JRE? 使用1.5和1.6编译的代码是否会在1.4 JRE上运行? We weren't sure which Java versions the 1.4 JRE supports. 我们不确定1.4 JRE支持哪些Java版本。

We know that if the code in question implements 1.5 or 1.6 supported features then it definitely won't compile ... and that there are some risks with "backwards compiling" but wasn't sure if the 1.4 JRE would refuse to even load the 1.5/1.6 compiled classes or not. 我们知道,如果有问题的代码实现了1.5或1.6支持的功能,那么肯定不会编译 ......而且“向后编译”存在一些风险 ,但不确定1.4 JRE是否会拒绝加载1.5 / 1.6编译类或不编译。


Update : I confirmed you get a java.lang.UnsupportedClassVersionError exception if you run an 1.6 class file on JRE 1.4. 更新 :如果您在JRE 1.4上运行1.6类文件,我确认您收到了java.lang.UnsupportedClassVersionError异常。

You can cross-compile. 你可以交叉编译。 This document shows you how: 本文档向您展示如何:

http://java.sun.com/javase/6/docs/technotes/tools/solaris/javac.html#crosscomp-example http://java.sun.com/javase/6/docs/technotes/tools/solaris/javac.html#crosscomp-example

You must specify the specific major version you're targeting (1.4, it sounds like). 您必须指定您要定位的特定主要版本(1.4,听起来像)。

Using this technique, your best bet is to always use the newest javac you can find! 使用这种技术,您最好的选择是始终使用您能找到的最新javac! That way you have all the latest bug fixes and performance improvements, and it's perfectly safe. 这样您就可以获得所有最新的错误修复和性能改进,而且非常安全。

EDIT: note that this does address the problem of library incompatibilities, which was discussed in several answers! 编辑:请注意,这确实解决了库不兼容问题,这在几个答案中讨论过!

Only if you compile with javac -target 1.4 switch. 仅当您使用javac -target 1.4开关编译时。

Obviously you will not be able to use 1.5+ features, such as Generics, Executors, etc. 显然,您将无法使用1.5+功能,例如Generics,Executors等。

If you just build with the defaults of javac 1.6, your class files will not work on old versions of Java. 如果您只使用默认的javac 1.6构建,那么您的类文件将无法在旧版本的Java上运行。

The best way to compile for older Java is just to use the older JDK releases . 编译旧Java的最佳方法就是使用较旧的JDK版本 But if you really want to try to compile for older Java from newer, here are some instructions: 但是,如果您真的想尝试从较新的Java编译旧Java,这里有一些说明:

I don't think it will. 我认为不会。

Occasionally (for reasons too complicated to explain), I try to run code in a 1.5 JRE that I compiled in a 1.6 JDK. 偶尔(由于原因太复杂而无法解释),我尝试在1.5 JRE中运行代码,我在1.6 JDK中编译。 It typically throws a java.lang.UnsupportedClassVersionError exception. 它通常会抛出java.lang.UnsupportedClassVersionError异常。

Yes and no. 是的,不是。 You can run code compiled under Java 6 on a 1.4 jvm if you set the "source" and "target" javac options to the version you are targeting (eg 1.4) when you compile them. 如果在编译时将“源”和“目标”javac选项设置为您要定位的版本(例如1.4),则可以在1.4 jvm上运行在Java 6下编译的代码。 This will work in cases where you have not used any of the additional classes or language features that were added since the target version. 这将适用于您未使用自目标版本以来添加的任何其他类或语言功能的情况。

Good luck. 祝好运。

Yes, you can produce class files that are compatible with 1.4 with the 1.6 compiler (javac) however, simply doing this is not necessarily going to produce code that will work. 是的,您可以使用1.6编译器(javac)生成与1.4兼容的类文件,但是,这样做不一定会产生可行的代码。 The problem is that that will still compile against the 1.6 version of the API. 问题是,仍然会针对1.6版本的API进行编译。

At first glance you would not expect this to be a problem since the contracts should not change but it is - I had a problem in that a new constructor that takes IIRC an integer was added to BigDecimal (in 1.5) and so at compile time the call to that constructor was specified however at runtime that constructor did not exist and so a runtime exception. 乍一看你不会期望这是一个问题,因为合同不应该改变,但它是 - 我有一个问题,一个新的构造函数,将IIRC一个整数添加到BigDecimal(在1.5),所以在编译时然而,在运行时指定了对该构造函数的调用,即构造函数不存在,因此运行时异常。 You're probably going to have issues like this when methods are overloaded and you're relying on auto variable conversion. 当方法过载并且您依赖于自动变量转换时,您可能会遇到类似问题。

The Javac app is in fact independent of the version of Java that it belongs to - you can specify a different API to use against 1.6 javac and in order to obviate any runtime issues this should be done. Javac应用程序实际上独立于它所属的Java版本 - 您可以指定一个不同的API来对抗1.6 javac,并且为了避免任何运行时问题,应该这样做。

Java 1.5 was a major release where it introduced enums,autoboxing and other stuffs.while compiling you will get exception saying unsupported class version.But if you compile with the command javac -source 1.4 -target 1._ claasname.java it will compile. Java 1.5是一个主要的版本,它引入了枚举,自动装箱和其他东西。在编译时你会得到异常说不支持的类版本。但是如果使用命令javac -source 1.4 -target 1._ claasname.java进行编译它将编译。

If you are using features of 1.5 like auto boxing and enums in that scenario it won't compile since these features are not available in 1.4.In this case we need to convert our code to older versions based on some tools. 如果您在该场景中使用1.5的自动装箱和枚举功能,则无法编译,因为这些功能在1.4中不可用。在这种情况下,我们需要根据某些工具将代码转换为旧版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM