0

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.

Community
  • 1
  • 1
Jose Renato
  • 705
  • 5
  • 18
  • Which exact interface from the Java API are you using? Normally, Oracle doesn't add methods to interfaces in the standard API, to avoid compatibility problems exactly like you describe. – Jesper May 13 '13 at 13:22
  • 1
    What is the problem to add 'getNewValue();' to your implementation class? Java won't kill if you added additional methos. – Reporter May 13 '13 at 13:26
  • The interface `java.sql.ResultSet` had this two operations added: ` T getObject( int columnIndex, Class type )` and ` T getObject( String columnLabel, Class type )` – Jose Renato May 13 '13 at 13:28
  • 1
    @JoseRenato: Don't use `Foo` example when your problem is with `java.sql.ResultSet`. You'll get much better help... – jlordo May 13 '13 at 13:31
  • @reporter I think you didnt understood the problem. Adding a new method is not the problem. The problem is, at the programing time, to know that a new operation was added on the interface. Imagine that you use 1000 diferent API interfaces, over diferent projects. The JAVA compability should ensure that a program that runs over JAVA 6 will work fine over JAVA 7. Dont you agree with that? – Jose Renato May 13 '13 at 13:45
  • @jlordo I didnt put the ResultSet interface directly in the question because I figured that with the abstraction would be easier to focus on the problem, that is the compatibility that was broken. Like Jesper said: "Oracle doesn't add methods to interfaces in the standard API, to avoid compatibility problems...". – Jose Renato May 13 '13 at 13:47
  • I don't get what this has to do with eclipse. – djechlin May 13 '13 at 13:56
  • @Jose I did understand your situation and agree with you. But you cannot prevent it. If the difference between two versions too big you won't have a choice to converting the code for the new version. That is the fate of a developer who would paid for. It was the same situation when Sun made a cut between Java 1.4 and Java 5, or the hardware industry decided to let die the floppy disc. – Reporter May 13 '13 at 13:57
  • @djechlin As I'm working with Eclipse I figured that someone had the same problem and created a solution using the environments configurations for example. – Jose Renato May 13 '13 at 14:05

1 Answers1

0

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216