Skip to main content

Allow use of 1.5 features when compiling code targeting older JDK versions

2 replies [Last post]
Joined: 2003-09-16

Right now, you cannot specify javac flag -source 1.5 without also specifying -target 1.5.

I can understand that some of the JDK1.5 features wouldn't work on older runtimes, but some of the features would work even on the older Java JVMs!

Enhanced for loops, autoboxing, varargs, annotations processing (in source scope) - all these useful features do not require a special new 1.5 JVM. Heck, even the generics will more or less work! And why not simply use StringBuffer instead of StringBuilder for joining strings when targeting old JDK versions? I dont care about reflection not being able to work with generics when running on old JVMs...

If any particular use of 1.5 feature would be impossible to implement on older JVM, error should be issued for that particular use of the feature, not disable the use of all features as a whole!

For example: I have a project that builds several verions of the library for different JDKs: 1.5, 1.4, 1.3, 1.2. I'd like to be able to use the annotations as part of the build process to control into what JARs to include given class, and for which Java versions it is intended (and with what -target option to compile it), cross-compiling from JDK 1.5 to 1.4, 1.3 and 1.2 . All this would be at source level and I don't see why would there be any problem with it compiling with -target 1.4 or less. I'm quite disappointed I can't use -source 1.5 -target 1.4. I cant use the 1.5 target comiled classes because of the classfile format version bump ;(

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-10

Retroweaver ( does what you want, and it is free and open-source (BSD).

God bless,
-Toby Reyelts

Joined: 2003-12-02

Get over it, you can't and never will have this.

Work to the lowest supported version. Your 1.4 code won't run on a 1.3 JVM either, nor will your 1.3 code run on a 1.2 JVM.

The -source xxx flag is there to compile to old classfile format IF POSSIBLE.
If you use language features that old classfile format doesn't support it's your responsibility to change your code, not the compiler's.