Domanda

How to setup Eclipse executions environments and projects to be developed for both JAVA 6 and JAVA 7 JREs?

I'm asking this because not all programs that are developed to run over JAVA 6 will run in JAVA 7.

In this situation for example:

I need to use a JAVA API interface, and the JAVA 6 version of this interface is:

public interface Foo {
    int getValue();
}

But, the JAVA 7 version of this interface is:

public interface Foo {
    int getValue();
    int getNewValue();
}

Now, if I code my program like this:

public class FooImp implements Foo {

    public static void main( String[] args ) {
    }

    public int getValue() {
        return 1;
    }
}

This will run work fine on JRE 6. But, if I run it over JRE 7 it will not work, because the Foo interface requires the operation getNewValue to be implemented.

In this case, what is the best solution, using Eclipse, to code my program and ensure that it will run over JRE 6 and 7? How should I configure the execution environments and run the tests?

Note: At this moment, I'm working like this:

  1. I code my program over JDK 6, to ensure that I'm not using any resourses of JDK 7 that doest exists in JDK 6.
  2. I run the first test over JRE 6.
  3. Then, it export my project and run it over JRE 7.

Note 2: Searching specifically about the java.sql.ResultSet problem, I found this question: Java 6 Source backward-compatibility and SQL

Now I see that, this problem is common with JDBC, the sourcecode compability is broken with frequency. So, I think that there is no much to discuss here. Thanks all for the answers and the effort.

Nessuna soluzione corretta

Altri suggerimenti

I don't know if there's an elegant way to do this, but the following could work.

  1. Reorganize and refactor your codebase into two parts:

    • the part that needs to work in the the Java 6 version of your codebase, and
    • the other code that is specific to the Java 7 version. (This is likely to involve creating some Java interfaces with Java 6 and Java 7 specific implementation classes.)
  2. Put the Java 6 and Java 7 code into separate Eclipse Java projects. Set the Eclipse project preferences to select the appropriate Java compliance level for each project; e.g. "Project > Properties > Java Compiler" ...

  3. If you haven't already done so, further refactor the code so that the Java 6 and Java 7 specific classes are dynamically loaded by the core; e.g. using Class.forName(...)

This should allow you to develop the Java 6 and Java 7 parts in the same Eclipse workspace.

You should also be able to use "Run > Run Configurations" and/or "Run > Debug Configurations" to create launchers for Java 6 and Java 7 for in-Eclipse testing.


Note: I've not tried this myself. Please let us know if it works.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top