views:

203

answers:

4

I want to have a source file which can compile with javac / ecj set to Java 5 but not Java 6 (even if the underlying Java runtime is Java 6).

This is to be certain that the compiler level is set correctly in Eclipse 3.5 running with Java 6 installed, but where the result needs to run on a Java 5 installation.

For java 1.4 I could use "enum" as a variable name (which fails under Java 5 and later) but I cannot locate a similar approach for Java 5 versus 6 (and later).

Suggestions?

+2  A: 

Since JVMDI was removed and JVMPI was disabled in Java SE 6 (according to J2SE 6.0 release note), you could add a code using that API: it will not compile with J2SE 6.0, only 5.0. (as illustrated by this thread)

VonC
If I understand you correctly, this is a JRE difference, not a language change? So it will be dependent on the JRE used, not the compiler level? I do not want to use Java 5 while developing, just catch that the compiler level is incorrect.
Thorbjørn Ravn Andersen
@Thorbjørn: yes, I cannot find any actual code which could be compiled, only dynamic library loaded at runtime. So, for now, it is not an appropriate solution (I keep it in CW mode for archive)
VonC
A: 

Use @Override on a method implementing an interface. Java 5 won't allow this, whereas Java 6 will e.g.

public interface Foo {
  void bar ();
}


public class Baz implements Foo {
  @Override
  void bar () {}
}
Jeff Foster
Nice, but I need it the other way around - something Java 5 allows that Java 6 doesn't :( Know a similar trick?
Thorbjørn Ravn Andersen
Why not do this? If it fails, you know it's not 6...
Jeff
He want to have it fail when compiling with Java6, not the other way around.
BalusC
Probably because he doesn't want to have to take it out to then build under 5.
Alex Feinman
A: 

Not an answer to your question but an alternative to your approach: wouldn't it be possible to use a second builder, based on ant or maven, that you use on demand to create the final application or library? This build would use a real external Java 5 SDK and thus guarantee that the application/library runs in a Java5 environment.

Andreas_D
Yes. We have a build system, which builds artifacts on the right level etc. It takes time, however, and during development I often have to build with Eclipse on my PC and run on the development machine (which only has Java 5, not a PC). Hence, the need to be certain to use Java 5. We very frequently create new workspaces, as the Working Set facility in Eclipse is a joke.
Thorbjørn Ravn Andersen
Fully agree on Working Set feature. One workspace/project is what I'm doing too ;)
Andreas_D
+11  A: 

There's nothing in the Java language that was removed between JDK5 and 6. The only thing which was added, as has been said, was the @Override annotation being allowable on interface methods - no keywords. Hence you are left with library diferences as the only cause of breaking changes, I'm afraid.

These do exist, even in the core API; in an unusual fit of backwards-compatibility-breaking revelry they changed the signature of some methods on the ExecutorService interface. This was because the generic signatures of the methods were overly restrictive. This was a pure library change (although, being part of java.util, a pretty core library); nothing to do with any language-level modification.

For example, from JDK5 :

<T> T invokeAny(Collection<Callable<T>> tasks)

to JDK6:

<T> T invokeAny(Collection<? extends Callable<T>> tasks)

This means that any program which contained code implementing this interface in JDK5, would not have compiled against JDK6. A snippet is easy to create; just let your IDE create an empty implementation of the JDK5 interface and then build against JDK6.

Note: that the wildcard was added because the previous version would not have accepted a parameter like List<MyCallable<String>> (i.e. the collection being typed by some subclass of callable) whereas the later version does.

oxbow_lakes
+1 nice thinking
skaffman
Good catch. +1 (illustrated here http://old.nabble.com/Incompatible-API-change-between-Java-5-and-Java-6--td22983799.html)
VonC
I can attest to having been bitten by this - now we have to support two versions of some of our code, purely because of this library change
oxbow_lakes