Files
plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/base/JRE.java
2024-10-12 00:57:35 +08:00

64 lines
1.9 KiB
Java

/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.base;
import xyz.zhouxy.plusone.commons.util.StringTools;
/**
* JRE version
*/
public class JRE {
private static final int JAVA_8 = 8;
public static final int CURRENT_VERSION = getJre();
public static boolean isJava8() {
return CURRENT_VERSION == JAVA_8;
}
private static int getJre() {
String version = System.getProperty("java.version");
boolean isNotBlank = StringTools.isNotBlank(version);
if (isNotBlank && version.startsWith("1.8")) {
return JAVA_8;
}
// if JRE version is 9 or above, we can get version from
// java.lang.Runtime.version()
try {
return getMajorVersion(version);
} catch (Exception e) {
// assuming that JRE version is 8.
}
// default java 8
return JAVA_8;
}
private static int getMajorVersion(String version) {
if (version.startsWith("1.")) {
return Integer.parseInt(version.substring(2, 3));
} else {
int dotIndex = version.indexOf(".");
return (dotIndex != -1) ? Integer.parseInt(version.substring(0, dotIndex)) : Integer.parseInt(version);
}
}
private JRE() {
throw new IllegalStateException("Utility class");
}
}