【问题标题】:Checking the version of JRE used during run-time检查运行时使用的 JRE 版本
【发布时间】:2012-06-18 09:11:33
【问题描述】:

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

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

提前致谢。

【问题讨论】:

  • 标记java。 (提高acc rate)。

标签: version java


【解决方案1】:

使用

System.getProperty("java.version")

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

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

System.getProperty("java.runtime.version");

【讨论】:

    【解决方案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") 将不起作用...输出略有不同。

    【讨论】:

      【解决方案3】:

      你可以把它放到一个类的静态代码中,让它只运行一次: (这段代码不是我的,你可以参考这里:-Getting Java version at runtime

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-05
        • 1970-01-01
        • 1970-01-01
        • 2012-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-02
        相关资源
        最近更新 更多