use java 7 syntax to compile to java 5

Deadly 提交于 2019-12-10 19:18:24

问题


is there any way to use java 7 syntax and produce bytecode that works on 1.5? as far as i know, options -target and -source can't be different. checking if no new API was used also would be nice but is not crucial


回答1:


No, it is not. As far as I know, different -source and -target work, but only if the source is lower or equal the target (in order to provide backwards compatibility). There may be source converters that convert your java 7 code into older versions.




回答2:


There's a project called Retroweaver which allows you to write Java 5 syntax (including generics, etc.) and convert it to code that runs on Java 1.4 and older.

As far as I know there isn't anything like that to make code with Java 7 syntax work on older versions.




回答3:


No, if you use Java7 syntax, you can't compile that code with Java 5.

-source and -target works only if the syntax is compatible with -target version.




回答4:


No, it isn't possible. E.g. consider the new try-with-resources construct: this might set suppressed throwables, but this API is only available in Java SE 7+:

http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getSuppressed%28%29

This means older Java version wouldn't be able to run that code anyway.



来源:https://stackoverflow.com/questions/12675400/use-java-7-syntax-to-compile-to-java-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!