Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java program snippet which can compile with a Java 5 compiler on JRE 6 but not Java 6 compiler?

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?

like image 607
Thorbjørn Ravn Andersen Avatar asked Nov 19 '09 12:11

Thorbjørn Ravn Andersen


People also ask

Can we compile Java program with JRE?

No you can't develop java programs only with JRE. You will need JDK for compiling your programs. JRE provides only runtime environment,but JDK is something you will need to compile your code to make them executable by your JRE .

Can we compile Java program without JRE?

You can't run Java program without JVM. JVM is responsible in running a Java program, but the only file that can be executed by JVM is Java bytecode, a compiled Java source code.

Which is the file extension used for compile Java class 5?

The compiler produces a compiled class file with the same name as the public class or interface declaration; the file extension used for a compiled Java file is . class.

Which compiler is used for compiling Java program?

javac - Java programming language compiler.


2 Answers

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.

like image 157
oxbow_lakes Avatar answered Nov 06 '22 17:11

oxbow_lakes


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)

like image 25
VonC Avatar answered Nov 06 '22 16:11

VonC