Path: blob/master/test/functional/cmdline_options_testresources/src/JavaRuntimeVersion.java
6004 views
/*******************************************************************************1* Copyright (c) 2001, 2020 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122public class JavaRuntimeVersion {2324/**25* example java.runtime.version's:26* 1.8.0_272-b1027* 8.0.7.0 - pxa6480sr7-20200922_01(SR7)28* 11.0.10-internal29* 11.0.10+530* obsolete: pxa6460sr11-20120403_01 (SR11)31*/32public static void main(String[] args) {33String runTimeVersion = System.getProperty("java.runtime.version");3435System.out.println("java.runtime.version is:" + runTimeVersion);3637String[] properties = runTimeVersion.split("\\s|-");38String[] versionParts = properties[0].split("\\+");39if (versionParts.length > 2) {40throw new IllegalArgumentException ("invalid version: " + properties[0]);41}42String intialVersion = versionParts[0];43if (intialVersion.startsWith("1.8") || intialVersion.startsWith("8.")) {44// Java 8 can have an underscore45if (!intialVersion.matches("[1-9][0-9\\.]+") && !intialVersion.matches("[1-9][0-9\\.]+_[1-9][0-9]*")) {46throw new IllegalArgumentException ("invalid version: " + intialVersion);47}48} else {49if (!intialVersion.matches("[1-9][0-9\\.]+")) {50throw new IllegalArgumentException ("invalid version: " + intialVersion);51}52}5354if ((versionParts.length > 1) && !versionParts[1].matches("[1-9][0-9]*")) {55throw new IllegalArgumentException ("invalid build: " + versionParts[1]);56}5758System.out.println("Version: " + properties[0]);59for (int i = 1; i < properties.length; i++) {60System.out.println("optional: " + properties[i]);61}6263System.out.println("JavaRuntimeVersion Test OK");64}6566}676869