tl;dr
Runtime.version().toString()
See this code run at Ideone.com.
12.0.1+12
Java 9+
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.