检查运行时使用的JRE版本

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

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

提前致谢。

运用

System.getProperty("java.version") 

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

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

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

就像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.version") ,则它不起作用…输出略有不同。

你可以把它放到一个类的静态代码中,所以它只运行一次:(这段代码不是我的。你可以在这里参考: – 在运行时获取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