fill͡pant͡
fill͡pant͡

Reputation: 1165

Compile class with different compiler compliance-version than the rest in Eclipse

I have been trying to compile one perticular class of my project with a higher compiler compliance level than the rest of the project but i cant figure out how to do it.

I am compiling the project in Java 6 (for compatibility reasons) and there is a single class which requires Java 8 and will only be instantiated in the code (With reflection) if the version of Java it is running on is >=8.

The problem is that the class which is in Java 8 obviously does not compile in Java 6 so i would like to ask, if there is a way in Eclipse to compile this single class with Java 8 when the project is exported to a Runnable Jar.

There is always the option of compiling the project in Java 6 (without the java 8 class), exporting, and manually compiling that class with Java 8 and then inserting in to the Jar file manually which works fine but is not very convenient.

For example lets suppose that the project consists of the following:

A Common Interface:

public interface CommonInterface {

    public void foo();

}

A Java 8 class:

public class Java8Class implements CommonInterface {

    @Override
    public void foo() {
        Arrays.asList("Some", "Java", "8", "code").forEach(System.out::println);
    }

}

A Java 6+ class:

public class Java6PlusClass implements CommonInterface {

    @Override
    public void foo() {
        List<String> list = Arrays.asList("Some", "Java", "6", "code");
        for (String s : list) {
            System.out.println(s);
        }
    }

}

And the main program class:

public class Selector {

    public static void main(String[] args) throws ReflectiveOperationException{
        CommonInterface ci = getCorrectInstance();
        ci.foo();
    }

    private static CommonInterface getCorrectInstance() throws ReflectiveOperationException {
        String version = System.getProperty("java.version");
        Matcher m = Pattern.compile("\\d\\.(\\d)\\..+").matcher(version);
        String clazz = null;
        if (m.find()) {
            int jv = Integer.parseInt(m.group(1));
            if (jv == 8)
                clazz = "Java8Class";
            else if (jv >= 6)
                clazz = "Java6PlusClass";

        }
        if (clazz == null)
            return null;
        return (CommonInterface) Class.forName(clazz).newInstance();
    }

}

In the above example, is it possible to configure Eclipse such that all classes apart from "Java8Class" compile with Java 6 compliance and the "Java8Class" compiles with Java 8 compliance?

Thanks.

Upvotes: 1

Views: 127

Answers (1)

Laf
Laf

Reputation: 8215

You cannot do this in Eclipse, and I don't believe what you are trying to achieve is a good idea.

Here's what you could do instead:

  1. Change the code of this Java 8 class to something equivalent in Java 6 (if possible).
  2. Create a new project with this single class, and compile it with Java 8 compliance level. Specify this new jar file on the command line when launching your application, and it should do the trick.
  3. Have 2 different versions of your application (one that is Java 8 compliant, and the other being Java 6 compliant), and launch the correct one based on the Java version available on the system.

I'm not totally sure if that second option would work simply by putting the jar file in the classpath in the cases where your program is executed using a Java 6 JRE. You might have to do some class loader trickery if it fails to start.

Upvotes: 1

Related Questions