1 package org.codehaus.mojo.jaxb2.shared; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 /** 23 * Helper to extract the runtime Java version from the System.properties. 24 * 25 * @author <a href="mailto:lj@jguru.se">Lennart Jörelid</a> 26 * @since 2.4 27 */ 28 public final class JavaVersion { 29 30 private static final String JAVA_VERSION_PROPERTY = "java.specification.version"; 31 32 /** 33 * Retrieves the major java runtime version as an integer. 34 * 35 * @return the major java runtime version as an integer. 36 */ 37 public static int getJavaMajorVersion() { 38 39 final String[] versionElements = System.getProperty(JAVA_VERSION_PROPERTY).split("\\."); 40 final int[] versionNumbers = new int[versionElements.length]; 41 42 for (int i = 0; i < versionElements.length; i++) { 43 try { 44 versionNumbers[i] = Integer.parseInt(versionElements[i]); 45 } catch (NumberFormatException e) { 46 versionNumbers[i] = 0; 47 } 48 } 49 50 /* 51 Java versions 1 - 8 (i.e. jdk 1.1 through 1.8) yields the structure 1.8 52 Java versions 9 - yields the structure 10 53 54 JDK 10 55 ====== 56 [java.specification.version]: 10 57 58 JDK 8 59 ===== 60 [java.specification.version]: 1.8 61 62 JDK 7 63 ===== 64 [java.specification.version]: 1.7 65 */ 66 return versionNumbers[0] == 1 ? versionNumbers[1] : versionNumbers[0]; 67 } 68 69 /** 70 * Checks if the runtime java version is JDK 8 or lower. 71 * 72 * @return true if the runtime java version is JDK 8 or lower. 73 */ 74 public static boolean isJdk8OrLower() { 75 return getJavaMajorVersion() <= 8; 76 } 77 }