如何在 Java 11 中生成代码,但针对 Java 8 及更高版本? [英] How to produce code in Java 11, but target Java 8 and above?
问题描述
我正在开发一个小型库,出于显而易见的原因,我想使用所有 Java 11 功能(除了我现在猜测的模块)生成代码,但我希望该库与 Java 8 及更高版本兼容.
I am working on a small library and for obvious reasons I would like to produce code using all the Java 11 features (except modules I guess for now), but I would like the library to be compatible with Java 8 and above.
当我尝试这个时:
javac -source 11 -target 1.8 App.java
我收到以下消息:
warning: source release 11 requires target release 11
...当我查看字节码时,我看到类的版本是 0x37
(Java 11):
...and when I look at the byte code I see that the version of the class is 0x37
(Java 11):
$ xxd App.class
00000000: cafe babe 0000 0037 ...
Java 8 无法加载它:
And Java 8 cannot load it:
Exception in thread "main" java.lang.UnsupportedClassVersionError: App has been
compiled by a more recent version of the Java Runtime (class file version 55.0),
this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
人们如何提供这种兼容性?我对所有构建工具持开放态度.
How do people provide such compatibility? I am open to all build tools.
对我来说,将高级语言(Java)转换为低级(字节码)似乎很容易.在我看来,当高级语言发生变化时,低级语言应该保持不变.这就是为什么我认为这是可能的.
For me it seems easy just to transform high-level language (Java) into low-level (bytecode). It appears to me that when the high-level language changes, the low-level should stay the same. That is why I thought it was possible.
更新
伙计们,我不认为这个答案重复了移至 OpenJDK-11 但在 Java 8 中编译,因为OP 询问如何继续生成具有 Java 8
功能的代码,但目标是 Java 11(这只是一个著名的向后兼容性).我的问题是相反的:我想在 Java 11 中生成代码,但目标是 Java 8.我在提出问题之前研究该主题时遇到了这个问题.我发现它不适用于我的情况.
Guys, I don't think that this answer duplicates Move to OpenJDK-11 but compile in Java 8, because there the OP asks how to keep producing the code with Java 8
features, but target Java 11 (which is just a famous backward compatibility). My question is the other way around: I want to produce the code in Java 11, but target Java 8. I came across that question when I was researching the topic before posing the question. I didn't find it applicable to my situation.
另一个问题 Java 8 代码是否可以编译为在 Java 7 JVM 上运行 看起来与我的问题相似,但是它在 2013 年被问到,字节码在 Java 7 和 Java 8 之间明显发生了变化.
The other question Can Java 8 code be compiled to run on Java 7 JVM does look similar to my question, but it was asked in 2013 and the bytecode obviously changed between Java 7 and Java 8.
我认为字节码自 Java 8 以来变化不大,这就是我问这个问题的原因.
I didn't think the bytecode changed that much since Java 8 that is why I asked this question.
推荐答案
虽然理论上可以使用复杂的工具将为 JDK 11 编译的类转换为 JDK 8,但这并非易事.二进制级别有显着变化.
While conversion of classes compiled for JDK 11 to JDK 8 would be theoretically possible with a sophisticated tool, it’s not trivial. There are significant changes on the binary level.
首先,JDK 11 引入了 nest 类型,在访问内部/外部类的 private
成员时无需生成合成访问器方法.当然,这种访问在旧版本中会失败.
First, JDK 11 introduced nest types, which eliminates the need to generate synthetic accessor methods when accessing private
members of inner/outer classes. Of course, such access would fail in older versions.
它还引入了动态常量,但我不知道 Java 语言是否在任何地方利用了该特性.这主要用于未来的版本.
It also introduced dynamic constants, though I don’t know whether the Java language exploits that feature anywhere. This is mainly intended for future versions.
然后,从 JDK 9 开始,字符串连接使用 invokedynamic
编译,参考 java.lang.invoke.StringConcatFactory
在 Java 8 中不存在.
Then, since JDK 9, string concatenation gets compiled using invokedynamic
referring to java.lang.invoke.StringConcatFactory
which is not present in Java 8.
一个可以工作的特性是接口中的私有
方法,在 Java 9 中作为语言特性引入,但在 Java 8 中已经在二进制级别处理.
A feature that could work, is private
methods in interfaces, introduced in Java 9 as a language feature, but already handled on the binary level in Java 8.
Java 8 也无法处理模块定义,但我想,它们会被忽略.
Java 8 would also be unable to process module definitions, but I suppose, they would be ignored.
这篇关于如何在 Java 11 中生成代码,但针对 Java 8 及更高版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!