Returns whether the current version of the JVM is at least that specified for major and minor version numbers. - Java Java Virtual Machine

Java examples for Java Virtual Machine:Version

Description

Returns whether the current version of the JVM is at least that specified for major and minor version numbers.

Demo Code

/*/*  ww  w.  ja  v a 2s.c  om*/
 * MiscUtils.java
 *
 * Copyright (C) 2002-2013 Takis Diakoumis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
//package com.java2s;

import java.util.ArrayList;

import java.util.List;

import java.util.StringTokenizer;

public class Main {
    /**
     * Returns whether the current version of the JVM is at least
     * that specified for major and minor version numbers. For example,
     * with a minium required of 1.4, the major version is 1 and minor is 4.
     *
     * @param major - the major version
     * @param minor - the minor version
     * @return whether the system version is at least major.minor
     */
    public static boolean isMinJavaVersion(int major, int minor) {
        //String version = System.getProperty("java.vm.version");
        String version = System.getProperty("java.version");
        String installedVersion = version;

        int index = version.indexOf("_");
        if (index > 0) {
            installedVersion = version.substring(0, index);
        }

        String[] installed = splitSeparatedValues(installedVersion, ".");

        // expect to get something like x.x.x - need at least x.x
        if (installed.length < 2) {
            return false;
        }

        // major at position 0
        int _version = Integer.parseInt(installed[0]);
        if (_version < major) {
            return false;
        }

        _version = Integer.parseInt(installed[1]);
        if (_version < minor) {
            return false;
        }

        return true;
    }

    /**
     * Returns a <code>String</code> array of the the CSV value
     * specified with the specified delimiter.
     *
     * @param csvString the CSV value
     * @param delim the delimiter used in the CSV value
     * @return an array of split values
     */
    public static String[] splitSeparatedValues(String csvString,
            String delim) {
        StringTokenizer st = new StringTokenizer(csvString, delim);
        List<String> list = new ArrayList<String>(st.countTokens());

        while (st.hasMoreTokens()) {
            list.add(st.nextToken());
        }

        String[] values = (String[]) list.toArray(new String[list.size()]);
        return values;
    }
}

Related Tutorials