Gradle changes JRE after "Gradle Refresh/Synch" in eclipse project

We are using latest org.eclipse.buildship.core plugin (3.1.9) in our custom Eclipse plugin code. Using this Eclipse plugin, we are creating gradle-based projects. Additionally, while creating projects, we are internally building gradle projects using this GradleCore.getWorkspace().createBuild(builder.build()); build.synchronize(monitor); code. But while creating a Gradle-based Java project using Java 11, Gradle synchronize caused it to change to Java 21 in the JRE System Library in Eclipse. After creating the project, it should show Java 11 in the JRE System Library, but it shows Java 21.

Also another thing I found that If I manually change Java System Library to Java 11, and then refreshing Gradle by right-clicking on the project, it will automatically update to Java 21.

So is there any way to resolve this issue by code or any other way?

For more information, I have attached my build.gradle file and screenshots.

Before Gradle Refresh
image

After Gradle Refresh :
image

build.gradle (Download and extract build.zip)
build.zip (1.3 KB)

By configuring a toolchain in your build.gradle, the Java version should remain stable:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
    }
}

@oleosterhagen This solution seems to be specific to a particular Java version, which is not desirable. We have our own custom Eclipse plugin extension that allows us to create a Gradle-based Java project. We are also providing a JDK selection option when creating a Java project. So basically, if the user creates a Gradle-based Java project using Java 17 through our plugin extension, the JRE system path should take Java 17 instead of Java 11(Using your solution its taking java 11).

Basically, the JRE system path should display the Java version depending on the selected Java version by the user. So is there any workaround for this?

Other Gradle project creation applications like Spring Initializr generate customized build.gradle files based on user input. For example, when the user chooses Java 17 a corresponding toolchain configuration with JavaLanguageVersion.of(17) is generated. Using a template engine this should be quite easy.

When you do not want to generate a customized file, you can use project properties which can be set dynamically, e.g.

In build.gradle:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(javaLanguageVersion)
    }
}

In your Eclipse plugin:

BuildConfigurationBuilder builder = BuildConfiguration.forRootProjectDirectory(new File("/home/ole/src/demo"));
builder.overrideWorkspaceConfiguration(true);
builder.arguments(List.of("-PjavaLanguageVersion=11")); // set project property
BuildConfiguration configuration = builder.build();

GradleBuild build = GradleCore.getWorkspace().createBuild(configuration);
build.synchronize(new NullProgressMonitor());

I just discovered that you can also configure the name of the JRE System Library directly:

eclipse {
    jdt {
        javaRuntimeName = "JavaSE-11"
    }
}

The same using a project property "-PeclipseJavaRuntimeName=JavaSE-11":

eclipse {
    jdt {
        javaRuntimeName = eclipseJavaRuntimeName
    }
}

@oleosterhagen We appreciate your help with this solution, it’s working well for us. Thanks!! :slight_smile: