I have multiple version of jre's installed on my system. Is there any way i would know which JRE version is being used by the application during run time.
There might be possible that application may use lower version of JRE -"Not Sure"
Thanks in Advance.
Using
System.getProperty("java.version")
will return the version of the currently running JVM.
Sometimes this does not return anything (don't know why). In that case you can try:
System.getProperty("java.runtime.version");
Like a_horse_with_no_name said: you can do this by calling System.getProperty("java.version")
.
If you need this information to make sure that the program is started using the correct JVM version, then try this:
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...
}
}
}
All you have to do is change MAJOR_VERSION
and MINOR_VERSION
to the values that you want and recompile. I use it in my programs all the time and it works well. Warning: it doesn't work if you change System.getProperty("java.version")
with System.getProperty("java.runtime.version")
... the output is slightly different.
You can put this into static code of a class so it runs only once:
(This code is not mine. You can refer here:- 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));
}
And if that does not turn up..
String version = Runtime.class.getPackage().getImplementationVersion()
Other links you can refer:
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