javamavenjvm

Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled against Java 6?


Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled for compatibility with Java 6? I do not have the source code of the dependency. I have the compiled jar as a maven dependency.


Solution

  • Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled for compatibility with Java 6?

    Let's unpick this:

    In both subcases above above, you should be able to use the dependency in your Java 6 project if you run the project on a Java 7 JRE1. A Java 7 JRE can load and run classfiles compiled for Java 6. In one of the subcases, you will be loading classes with two (or more) class version numbers. But that is OK.

    On the other hand, if you try to run the code on a Java 6 JRE, then:


    The most advisable approach is to migrate your project and your execution platform to Java 7. Or better still to Java 8 or Java 11, since Java 7 is EOL'd

    If you can't do that, the next best thing would be to avoid using the Java 7 dependency ... until you can upgrade.

    If you have customers who insist they you continue to support Java 6, then they are impeding your ability to progress your product line and/or costing your extra effort to support them. They should be charged a premium for that2.

    If you have decided to avoid upgrading your Java platform for internal reasons, this decision is accumulating technical debt ... that your organization will need to "pay down" that debt in the long term.


    1 - .... or JDK. A JDK is equivalent to a JRE for the purposes of running code.
    2 - ... or refuse to accede to their demands. They will be in a worse position if they have to look for an alternative vendor that still supports Java 6!