javaeclipsegradlejarmulti-release-jar

How to setup an Eclipse project for create a multi-release jar file?


If I want build a multi-release jar file (MRJAR) then I need two source folder that compile with different java versions. How can I setup this in Eclipse?

I can configure the Gradle build script for such building but what is the best practice for Eclipse?

I expected/want that if I run the project with different Java versions that I can debug the right versions for the running Java version.


Solution

  • You will now (Q4 2025) find a better support for MRJAR with Eclipse 4.38:

    Multi-Release JAR Compilation Support

    Contributors:

    JDT now supports native compilation of Multi-Release JARs. Multi-Release JARs are a feature introduced in Java 9 that allows a single JAR file to contain multiple versions of the same class, each compiled for a specific Java release. This enables libraries to take advantage of newer Java features while maintaining backward compatibility with older Java versions.

    With this new feature, you can now compile Multi-Release Types directly within Eclipse. Multi-Release Types are classes that exist in different variants with the same name across different source folders, where each variant is automatically compiled with the appropriate Java release settings and placed in the correct output folder.

    This implementation follows the "Pattern 3: Single Project" approach described in the Maven Compiler Plugin documentation.
    Previously, this pattern had limited IDE support, but Eclipse now provides a simple and elegant solution by compiling individual class files with the corresponding release option.

    To enable Multi-Release compilation for a source folder, open the source folder properties in the Build Path configuration and specify the target Java release:

    Multi-Release JAR configuration dialog: https://eclipse.dev/eclipse/news/4.38/images/multi-release-jar-config.png

    Once configured, you can:

    • Edit and compile Multi-Release Types simultaneously across different Java versions
    • Run Multi-Release compiled projects directly from the Eclipse IDE
    • Use m2eclipse's built-in support to automatically map maven-compiler-plugin executions for Multi-Release JARs into JDT configuration

    This feature significantly simplifies the development workflow for libraries that need to support multiple Java versions. We welcome your feedback and testing of this new capability. For known issues and to report problems, please visit the JDT Core issue tracker.

    For more details on m2eclipse integration, see the m2e-core release notes.