Reputation: 247
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.
Upvotes: 2
Views: 4796
Reputation: 340070
Runtime.version().toString()
See this code run at Ideone.com.
12.0.1+12
The other Answers here are outdated as of Java 9 and Java 10.
Runtime.Version
class👉 Java 9 gained a class dedicated to representing the version of Java: Runtime.Version
.
To get an instance of that class, use the version
method added to the pre-existing class Runtime
.
Runtime.Version v = Runtime.version() ;
From there you interrogate the various pieces of version information.
// Required parts.
int feature = v.feature() ;
int interim = v.interim() ;
int update = v.update() ;
int patch = v.patch() ;
// Optional parts.
Optional<Integer> build = v.build() ;
Optional<String> pre = v.pre() ;
Optional<String> optional = v.optional() ;
See this code run at Ideone.com:
feature: 12
interim: 0
update: 1
patch: 0
build: Optional[12]
pre: Optional.empty
optional: Optional.empty
Note that major
, minor
, and security
are deprecated as of Java 10. Version numbering in Java was redefined in Java 10 and later. To quote the Javadoc:
The sequence may be of arbitrary length but the first four elements are assigned specific meanings, as follows:
$FEATURE.$INTERIM.$UPDATE.$PATCH
$FEATURE
— The feature-release counter, incremented for every feature release regardless of release content. Features may be added in a feature release; they may also be removed, if advance notice was given at least one feature release ahead of time. Incompatible changes may be made when justified.
$INTERIM
— The interim-release counter, incremented for non-feature releases that contain compatible bug fixes and enhancements but no incompatible changes, no feature removals, and no changes to standard APIs.
$UPDATE
— The update-release counter, incremented for compatible update releases that fix security issues, regressions, and bugs in newer features.
$PATCH
— The emergency patch-release counter, incremented only when it's necessary to produce an emergency release to fix a critical issue.The fifth and later elements of a version number are free for use by platform implementors, to identify implementor-specific patch releases.
FYI, Wikipedia maintains a history of Java versions. The Long-Term Support versions are 8, 11, and 17.
Upvotes: 1
Reputation: 4346
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
Upvotes: 0
Reputation: 10920
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.
Upvotes: 2
Reputation:
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");
Upvotes: 4