jbang icon indicating copy to clipboard operation
jbang copied to clipboard

`jbang init` selects wrong default Java version

Open maxandersen opened this issue 2 months ago • 1 comments

in #2262 it was found we might have a inconsistency in how java versions are tested/detected in jbang init.

proposed solution is at https://github.com/jbangdev/jbang/pull/2262#discussion_r2466488946, copied here for reference:

the code is this in Init.java:

int reqVersion = buildMixin.javaVersion != null ? JavaUtil.minRequestedVersion(buildMixin.javaVersion) : JavaUtil.getCurrentMajorJavaVersion();

Yeah, that is way too simplistic, this is what getCurrentMajorJavaVersion() does:

return parseJavaVersion(System.getProperty("java.version"));

That's way too simplistic. It's also a pretty old method that right now is only used for a couple of cases where we need to know what JDK we're running ourselves (for example to know if the JDK supports modules).

The better way is:

        Jdk.@NonNull InstalledJdk jdk = JavaUtil.defaultJdkManager().getOrInstallJdk(buildMixin.javaVersion);
        int reqVersion = jdk.majorVersion();

maxandersen avatar Oct 28 '25 09:10 maxandersen

Changed title to be less dramatic :-) It's only jbang init that has this issue after all. Also cleaned up the issue text a bit.

quintesse avatar Oct 28 '25 12:10 quintesse