哪些 JDK 发行版可以运行 `javac -source 1.6 -target 1.5`? [英] Which JDK's distributions can run `javac -source 1.6 -target 1.5`?

查看:26
本文介绍了哪些 JDK 发行版可以运行 `javac -source 1.6 -target 1.5`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:请不要评论交叉编译的所有危险.谢谢.

NOTE: Please do not comment on all the perils of cross-compiling. Thank you.

我有一种情况,我们需要为 Java 5 JVM 编译 Java 6 源代码(以确保 JAX-WS 使用正确).以前我们使用 ant ant 脚本(显然可以)完成此操作,但在迁移到 Maven 后,我们发现它以 javac 抱怨结束:

I have a situation where we need to have Java 6 source compiled for a Java 5 JVM (to be sure that JAX-WS usage is correct). Previously we have done this with ant ant script (which apparently can), but after migrating to Maven we have found that it ends up with javac complaining:

$ javac -source 1.6 -target 1.5
javac: source release 1.6 requires target release 1.6

是否有任何适用于 Linux(Ubuntu 11.10、x86)的 Java 发行版,javac 可以做到这一点?

Is there any Java distribution for Linux (Ubuntu 11.10, x86) where the javac can do this?

似乎不是,因为限制在 javac 中是相同的.解决方案(使这种需求消失)是从默认的 javac 编译器更改为 maven-compiler-plugin 中的 eclipse 编译器.

It appears not, as the limitation is in javac which is the same. The solution (which made this need go away) was to change from the default javac compiler to the eclipse compiler in maven-compiler-plugin.

我发现 Eclipse 编译器为 javadoc 实用程序不同意的匿名内部类生成字节码.我正在为此问题准备错误报告.

I've found that the Eclipse compiler generates byte code for anonymous inner classes that the javadoc utility disagrees with. I am preparing a bug report for this issue.

推荐答案

根据文档 (Java 5Java 6),当您按照 交叉编译示例.

According to the documentation (Java 5, Java 6), the Oracle SDK should be able to do this when you follow the instructions in the Cross-Compilation Example.

Java 6 应该支持 1.3 到 1.6 之间的任何版本作为 -target;它没有说明当您在源代码中使用泛型和其他兼容"功能时会发生什么.编译器应该能够剥离它们.

Java 6 should support any version between 1.3 to 1.6 as -target; it doesn't say anything what happens when you use generics and other "compatible" features in the source. The compiler should be able to strip them.

游戏中的另一个罪魁祸首可能是 javac:编译器可能能够处理这组参数,但命令行工具可能冒犯.

Another culprit in the game might be javac: The compiler might be able to handle this set of arguments but the command line tool might take offense.

在这种情况下,使用 Java 编译器 API.这可能允许使用一些其他方法无法实现的技巧.

In this case, write your own command line using the Java Compiler API. That might allow to pull of some tricks that you can't achieve otherwise.

你也可以试试Eclipse编译器(见使用批处理编译器").

You can also try the Eclipse compiler (see "Using the batch compiler").

这可能会因为 Java 的工作方式而失败:只要 X <= Y,Java X 代码就可以在 Java Y 上运行.因此,虽然您可以轻松地为 Java 6 VM 编译 Java 1.4 代码,但反过来并不总是正确的.

This might fail because of how Java works: Java X code can run on Java Y as long as X <= Y. So while you can easily compile Java 1.4 code for a Java 6 VM, the reverse is not always true.

如果其他一切都失败了,编写一个预处理器来读取源代码并去除不支持的元素(如接口上的 @Override).只要您不时使用 Java 6 编译带有注释的代码,转换后的代码也应该是安全的(除非您的代码剥离器有错误......)

If everything else fails, write a preprocessor that reads the source and strips unsupported elements (like @Override on interfaces). As long as you compile the code with the annotations once in a while with Java 6, the converted code should be safe as well (unless your code stripper has a bug ...)

这篇关于哪些 JDK 发行版可以运行 `javac -source 1.6 -target 1.5`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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