diff --git a/core/src/main/java/hudson/PluginWrapper.java b/core/src/main/java/hudson/PluginWrapper.java index 2860181482..b9cdd610db 100644 --- a/core/src/main/java/hudson/PluginWrapper.java +++ b/core/src/main/java/hudson/PluginWrapper.java @@ -35,6 +35,7 @@ import hudson.model.UpdateSite; import hudson.util.VersionNumber; import jenkins.YesNoMaybe; import jenkins.model.Jenkins; +import jenkins.util.java.JavaUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.LogFactory; import org.kohsuke.accmod.Restricted; @@ -762,8 +763,7 @@ public class PluginWrapper implements Comparable, ModelObject { String minimumJavaVersion = getMinimumJavaVersion(); if (minimumJavaVersion != null) { - // TODO replace with calls to Runtime.version() once we're on Java 9+ - VersionNumber actualVersion = new VersionNumber(System.getProperty("java.specification.version")); + VersionNumber actualVersion = JavaUtils.getCurrentJavaRuntimeVersionNumber(); if (actualVersion.isOlderThan(new VersionNumber(minimumJavaVersion))) { versionDependencyError(Messages.PluginWrapper_obsoleteJava(actualVersion.toString(), minimumJavaVersion), actualVersion.toString(), minimumJavaVersion); } diff --git a/core/src/main/java/hudson/model/UpdateSite.java b/core/src/main/java/hudson/model/UpdateSite.java index 89ae58d46b..739229faa8 100644 --- a/core/src/main/java/hudson/model/UpdateSite.java +++ b/core/src/main/java/hudson/model/UpdateSite.java @@ -70,6 +70,7 @@ import jenkins.model.DownloadSettings; import jenkins.security.UpdateSiteWarningsConfiguration; import jenkins.util.JSONSignatureValidator; import jenkins.util.SystemProperties; +import jenkins.util.java.JavaUtils; import net.sf.json.JSONArray; import net.sf.json.JSONException; import net.sf.json.JSONObject; @@ -1132,8 +1133,9 @@ public class UpdateSite { */ public boolean isForNewerJava() { try { + final VersionNumber currentRuntimeJavaVersion = JavaUtils.getCurrentJavaRuntimeVersionNumber(); return minimumJavaVersion != null && new VersionNumber(minimumJavaVersion).isNewerThan( - new VersionNumber(System.getProperty("java.specification.version"))); + currentRuntimeJavaVersion); } catch (NumberFormatException nfe) { logBadMinJavaVersion(); return false; // treat this as undeclared minimum Java version diff --git a/core/src/main/java/jenkins/util/java/JavaUtils.java b/core/src/main/java/jenkins/util/java/JavaUtils.java index 909c3e8457..38a28e72c0 100644 --- a/core/src/main/java/jenkins/util/java/JavaUtils.java +++ b/core/src/main/java/jenkins/util/java/JavaUtils.java @@ -23,6 +23,7 @@ */ package jenkins.util.java; +import hudson.util.VersionNumber; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -42,7 +43,7 @@ public class JavaUtils { * @return {@code true} if it is Java 8 or older version */ public static boolean isRunningWithJava8OrBelow() { - String javaVersion = System.getProperty("java.specification.version"); + String javaVersion = getCurrentRuntimeJavaVersion(); return javaVersion.startsWith("1."); } @@ -51,8 +52,23 @@ public class JavaUtils { * @return {@code true} if it is Java 9 or above */ public static boolean isRunningWithPostJava8() { - String javaVersion = System.getProperty("java.specification.version"); + String javaVersion = getCurrentRuntimeJavaVersion(); return !javaVersion.startsWith("1."); } + /** + * Returns the JVM's current version as a {@link VersionNumber} instance. + */ + public static VersionNumber getCurrentJavaRuntimeVersionNumber() { + return new VersionNumber(getCurrentRuntimeJavaVersion()); + } + + /** + * Returns the JVM's current version as a {@link String}. + * @see System#getProperty(String) + */ + public static String getCurrentRuntimeJavaVersion() { + // TODO: leverage Runtime.version() once on Java 9+ + return System.getProperty("java.specification.version"); + } }