1

我的系统上安装了多个版本的 jre。有什么方法可以知道应用程序在运行时使用的是哪个 JRE 版本。

应用程序可能使用较低版本的 JRE -“不确定”

提前致谢。

4

3 回答 3

4

使用

System.getProperty("java.version")

将返回当前运行的 JVM 的版本。

有时这不会返回任何东西(不知道为什么)。在这种情况下,您可以尝试:

System.getProperty("java.runtime.version");
于 2012-06-18T09:29:11.277 回答
2

就像 a_horse_with_no_name 所说:你可以通过调用System.getProperty("java.version").

如果您需要此信息以确保使用正确的 JVM 版本启动程序,请尝试以下操作:

public class Version
{
    //specifies minimum major version. Examples: 5 (JRE 5), 6 (JRE 6), 7 (JRE 7) etc.
    private static final int MAJOR_VERSION = 7;

    //specifies minimum minor version. Examples: 12 (JRE 6u12), 23 (JRE 6u23), 2 (JRE 7u2) etc.
    private static final int MINOR_VERSION = 1;

    //checks if the version of the currently running JVM is bigger than
    //the minimum version required to run this program.
    //returns true if it's ok, false otherwise
    private static boolean isOKJVMVersion()
    {
        //get the JVM version
        String version = System.getProperty("java.version");

        //extract the major version from it
        int sys_major_version = Integer.parseInt(String.valueOf(version.charAt (2)));

        //if the major version is too low (unlikely !!), it's not good
        if (sys_major_version < MAJOR_VERSION) {
            return false;
        } else if (sys_major_version > MAJOR_VERSION) {
            return true;
        } else {
            //find the underline ( "_" ) in the version string
            int underlinepos = version.lastIndexOf("_");

            try {
                //everything after the underline is the minor version.
                //extract that
                int mv = Integer.parseInt(version.substring(underlinepos + 1));

                //if the minor version passes, wonderful
                return (mv >= MINOR_VERSION);

            } catch (NumberFormatException e) {
                //if it's not ok, then the version is probably not good
                return false;
            }
        }
    }

    public static void main(String[] args)
    {
        //check if the minimum version is ok
        if (! isOKJVMVersion()) {
            //display an error message
            //or quit program
            //or... something...
        }
    }
}

您所要做的就是更改您想要的值并重新编译MAJOR_VERSIONMINOR_VERSION我一直在我的程序中使用它并且效果很好。System.getProperty("java.version")警告:如果您更改为System.getProperty("java.runtime.version")... 输出将略有不同,则它不起作用。

于 2012-06-18T09:40:48.180 回答
0

您可以将其放入类的静态代码中,使其仅运行一次:(此代码不是我的。您可以在此处参考:-在运行时获取 Java 版本

public static double JAVA_VERSION = getVersion ();

public static double getVersion () {
    String version = System.getProperty("java.version");
    int pos = version.indexOf('.');
    pos = version.indexOf('.', pos+1);
    return Double.parseDouble (version.substring (0, pos));
}

如果那没有出现..

String version = Runtime.class.getPackage().getImplementationVersion()

您可以参考的其他链接:

http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html http://docs.oracle.com/javase/1.5.0/docs/relnotes/version-5.0.html

于 2016-04-20T11:24:27.727 回答