简体   繁体   English

为什么javac无法编译Java 1.5代码以在Java 1.4 JVM上运行?

[英]Why can't javac compile Java 1.5 code to run on Java 1.4 JVMs?

Why can't the Java compiler compile Java 1.5 source code (with for-each loops for instance) to Java 1.4 bytecode? 为什么Java编译器不能将Java 1.5源代码(例如for-each循环)编译为Java 1.4字节码?

I know that you can provide a -target 1.4 switch, which tells the compiler to produce 1.4 compliant bytecode, but this requires -source 1.4 as well. 我知道你可以提供一个-target 1.4开关,它告诉编译器产生1.4兼容的字节码,但这也需要-source 1.4

$ javac -target 1.4 -source 1.5 Test.java
javac: source release 1.5 requires target release 1.5

I'm taking a course in compiler construction now, and as I understand it, compilers transform the source code into an intermediate representation anyway. 我现在正在编写一个编译器构建课程,据我所知,编译器无论如何都将源代码转换为中间表示。 Why can't such intermediate representation be output as 1.4 compliant bytecode? 为什么这样的中间表示不能输出为1.4兼容字节码? It sounds like an easy enough task, since, for each loops, varargs etc are basically just syntactic sugar! 这听起来像一个简单的任务,因为,对于每个循环,varargs等基本上只是语法糖!

(Note that I can see that API classes introduced in Java 1.5 obviously can't be referred to when executing on a 1.4 JVM. I'm still interested in the situation in which you stick to the 1.4 API.) (请注意,我可以看到,在1.4 JVM上执行时,显然无法引用Java 1.5中引入的API类。我仍然对您坚持使用1.4 API的情况感兴趣。)

Because Java 1.5 provides features that are simply not present in a 1.4 VM. 因为Java 1.5提供了1.4 VM中根本不存在的功能。

What should the compiler do if your source contains generics? 如果您的源包含泛型,编译器应该怎么做? Or an enum definition? 还是enum定义? What if it does autoboxing? 如果它进行自动装箱怎么办?

There are workarounds for all of these issues, but it's not the job of the Java compiler to implement workarounds. 所有这些问题都有解决方法,但实现变通方法并不是Java编译器的工作。 Instead you either need to port your source to a Pre-Java-5 level or use a tool such as Retroweaver (there's a more modern replacement for that out there, but I keep forgetting its name, since luckily I no longer need to use it). 相反,您需要将源代码移植到Pre-Java-5级别或使用诸如Retroweaver之类的工具(那里有一个更现代的替代品,但我一直忘记它的名字,因为幸运的是我不再需要使用它)。

Also note that Java 1.5 code that doesn't use any of the new features ( enum , auto-boxing, generics) most likely can be compiled using -source 1.4 . 还要注意的是,不使用任何新功能(Java 1.5的代码enum ,自动装箱,泛型)最有可能可以使用编译-source 1.4

You are correct that some enhancements in Java 1.5 did not involve the introduction of any new bytecodes or class file format changes. 您是正确的,Java 1.5中的一些增强功能不涉及引入任何新的字节码或类文件格式更改。 There are others like annotations and enums that did. 还有其他像注释和枚举。 Therefore arbitrary Java 1.5 source cannot be compiled to a valid Java 1.4 class. 因此,任意Java 1.5源代码都无法编译为有效的Java 1.4类。 That being said there is a project named retroweaver that aims to transform Java 1.5 class files to Java 1.4 class files available at http://retroweaver.sourceforge.net/ . 话虽如此,有一个名为retroweaver的项目旨在将Java 1.5类文件转换为http://retroweaver.sourceforge.net/上提供的Java 1.4类文件。

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

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