View Javadoc
1   package org.slf4j.log4j12;
2   
3   import java.lang.reflect.Method;
4   
5   import org.slf4j.helpers.Util;
6   
7   public class VersionUtil {
8       // code was compiled under Java 8 or later
9       static final int MINIMAL_VERSION = 8;
10  
11      static public int getJavaMajorVersion() {
12          String javaVersionString = Util.safeGetSystemProperty("java.version");
13          return getJavaMajorVersion(javaVersionString);
14      }
15  
16      static public int getJavaMajorVersion(String versionString) {
17          if (versionString == null)
18              return MINIMAL_VERSION;
19          if (versionString.startsWith("1.")) {
20              return versionString.charAt(2) - '0';
21          } else {
22              // we running under Java 9 or later
23              try {
24                  Method versionMethod = Runtime.class.getMethod("version");
25                  Object versionObj = versionMethod.invoke(null);
26                  Method majorMethod = versionObj.getClass().getMethod("major");
27                  Integer resultInteger = (Integer) majorMethod.invoke(versionObj);
28                  return resultInteger.intValue();
29              } catch (Exception e) {
30                  return MINIMAL_VERSION;
31              }
32          }
33      }
34  }