At this link there is discussion of version numbers. There is no explanation there, or anywhere else I found, of what these macro-looking things are. For example, in what context is "$VNUM" used? What is its scope? Is it defined in a C++ header file used during openJDK compilation? Is it accessible from within my java program? Is it some kind of environment variable?
Thank you for any explanation.
The $
followed by a name is a standard way to make use of variables in Unix shell scripts and command line prompts. The JEP to which you’ve linked is not actually referring to a Unix command, but rather is using that syntax to indicate how certain values are arranged.
If you open a terminal right now in Linux or OS X, you can enter something like this to see how the variable replacement works:
MAJOR=11
MINOR=0
SECURITY=3
echo $MAJOR.$MINOR.$SECURITY
The author could have described it using Java syntax, like major + "." + minor + "." + security
, or could have described it with a BNF-like notation, but chose to do it this way instead.
The version elements are used by instances of the Runtime.Version class. You can obtain an instance for the currently running JVM using the Runtime.version() method.
Why would you want to look at it? To work around known bugs in a particular version, or to make use of newer APIs when available.
If you wanted to get the value that the JEP describes as $VNUM
, you would write:
String vnum = Runtime.version().version().stream().map(Object::toString).collect(
Collectors.joining("."));
…which concatenates the major/minor/security numbers (which actually are not called that anymore) with a period in between.
Usually, however, you won’t need that string. You’re more likely to compare the version, to test for a particular bug or capability:
Runtime.Version requiredVersion = Runtime.Version.parse("11");
if (Runtime.version().compareTo(requiredVersion) < 0) {
System.err.println("This program requires Java 11 or later.");
System.exit(1);
}