Javac 与 1.7 交叉编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18320587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javac Cross-Compilation with 1.7
提问by Eyad Ebrahim
So guys,
所以伙计们,
I'm trying to play a bit with Javac Cross compilation with Ant and on terminal. Locally and on an integration environment and i'm having the same problem on the very basic problem.
我正在尝试使用 Ant 和终端进行 Javac 交叉编译。在本地和集成环境中,我在非常基本的问题上遇到了同样的问题。
I run this in the linux terminal (and also on my cygwin on windows and the cmd):
我在 linux 终端中运行它(也在 Windows 和 cmd 上的 cygwin 上运行):
javac -target 1.6 -source 1.7 -bootclasspath /usr/java/jdk1.6.0_27/jre/lib/rt.jar Main.java
with Main.java with nothing other than a System.out.println.
Main.java 除了 System.out.println 之外别无他物。
javac -version ==> javac 1.7.0_11
I'm getting the error message:
我收到错误消息:
javac: source release 1.7 requires target release 1.7
I have roughly the same configuration on my local windows machine with the exact same results.
我在本地 Windows 机器上的配置大致相同,结果完全相同。
It was my understanding that cross compilation is all about compiling some source code that is compatible with a higher version jdk using that higher version of jdk, but passing the rt.jar of the target version that is supposedly lower.
我的理解是,交叉编译就是使用更高版本的 jdk 编译一些与更高版本 jdk 兼容的源代码,但传递目标版本的 rt.jar 应该是较低的。
if target and source are the same, it worked.
如果目标和源相同,则有效。
target=1.7 and source=1.7 workd fine
target=1.6 and source=1.6 worked just fine
but i want cross-compilation, so what is it that i'm doing wrong?
但我想要交叉编译,所以我做错了什么?
I appreciate all the help I could get and thanks in advance.
我感谢我能得到的所有帮助,并提前致谢。
采纳答案by vinay
You cannot have a newer version of source and lower version of target. For example, In Java 5, a number of new features were added to the language, such as generics, autoboxing and you cannot expect a JVM 1.4 to understand it. So, you must tell the compiler that your source code is Java 1.4 source code. This explains the results you have.
您不能拥有较新版本的源和较低版本的目标。例如,在 Java 5 中,该语言添加了许多新功能,例如泛型、自动装箱,您不能指望 JVM 1.4 能够理解它。因此,您必须告诉编译器您的源代码是 Java 1.4 源代码。这解释了您的结果。
The default for -target depends on the value of -source:
-target 的默认值取决于 -source 的值:
- If -source is not specified, the value of -target is 1.7
- If -source is 1.2, the value of -target is 1.4
- If -source is 1.3, the value of -target is 1.4
- If -source is 1.5, the value of -target is 1.7
- If -source is 1.6, the value of -target is 1.7
- For all other values of -source, the value of -target is the value of -source.
- 如果未指定 -source,则 -target 的值为 1.7
- 如果 -source 为 1.2,则 -target 的值为 1.4
- 如果 -source 为 1.3,则 -target 的值为 1.4
- 如果 -source 为 1.5,则 -target 的值为 1.7
- 如果 -source 为 1.6,则 -target 的值为 1.7
- 对于 -source 的所有其他值,-target 的值就是 -source 的值。
For more info refer to http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
有关更多信息,请参阅http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
回答by Thorbj?rn Ravn Andersen
This is a limit in javac. Note that you could get away with just specifying "-target" (and not -source) in older versions of javac. You might still be able to.
这是 javac 中的一个限制。请注意,您可以在旧版本的 javac 中指定“-target”(而不是 -source)。你可能仍然可以。
You may want to consider using the Eclipse Java Compiler (ecj) which is available as a standalone compiler, as a maven plugin and which also can be used by the javac task in ant scripts.
您可能需要考虑使用 Eclipse Java 编译器 (ecj),它可以作为独立编译器、maven 插件使用,也可以由 ant 脚本中的 javac 任务使用。
See http://help.eclipse.org/indigo/topic/org.eclipse.jdt.doc.user/tasks/task-using_batch_compiler.htmfor details.
有关详细信息,请参阅http://help.eclipse.org/indigo/topic/org.eclipse.jdt.doc.user/tasks/task-using_batch_compiler.htm。