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

- 13,982
- 13
- 79
- 165
4 Answers
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.

- 1,729
- 1
- 15
- 34
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.

- 202,709
- 46
- 318
- 350
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.

- 65,990
- 13
- 130
- 167
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.

- 37,247
- 13
- 80
- 152