Using Bash to Determine Maven's Java Version

Very Quick Summary

This html document leads up to a short script (included at the bottom) that uses GNU sed to determine which version of java is in use by maven. It could easily be changed into detecting what java version is used by anything or anyone on your machine.

Start point

Version numbering in Java isn't easy. Back in May 2000, when Java 1.3 was released with significant improvement (Swing was included, among other things), the version number should have been bumped to 2, in my opinion. But it wasn't, and the following Javas all kept within the 1.X boundaries, despite having quite important new features, sometimes. One could easily defend bumping the main version number when adding generics (1.5) or lambdas (1.8), in my opinion.

The release of Java 9 changed this. In 2017. Asking for java -version now did not return 1.9.something, but simply 9. Finally.

But then, all scripts that looked up java versions had to be updated. And as Java 10 was released in 2018, some of the initial quick fixes for such scripts ceased to work, since the number 10 commences with the same digit as a version number like «1.8».

Here's where I start: I have a bash script starting five different java processes that together make up a whole. When started with Java 9 (or later releases), they need the addition of command line parameter --add-modules=java.xml.bind. But this cannot be added blindly, since earlier Java versions will refuse to start when this parameter is added. So my bash script needed a simple if-then test.

Google is Your Friend (Mostly)

Since I am a fond reader of the O'RLY? books Trying Stuff Until it Works and Copying and Pasting from Stack Overflow, I turned to uncle DuckDuck. He found this SO post for me: Correct way to check Java version from BASH script.

One of the scripts that I stole from SO worked really fine. For a while. But then it failed. After an unfortunate removal and reinstallation of different versions of Java and Maven (I no longer know what went where and which piece of software was removed and/or re-added first and last), there was a mismatch that made my starter script crash, and I didn't immediately spot it because Maven has its own idea about where to find the Java binaries, and I checked java -version in the startup script, not mvn -version.

So when I realised that I had checked the wrong setting, I could reliably find the Java version actually used to launch my Maven-managed jar files. But the script I had found, which detected any java version commencing with «1.» AND the version commencing with «9», failed to find the correct version number for Java 10. So I needed to rewrite the script. Luckily, sed is invented. Here's how the script looks:

Script

# Check that maven is installed, otherwise abort:                                                                                                                                                     
hash mvn 2>/dev/null || { echo -e >&2 "Maven is required, but it does not seem to be installed."; exit 1; }

mvnversion=$(mvn -version 2>&1)
javaversion=$(echo $mvnversion | sed -n 's/.*\([Jj]ava version[[:space:]:"]*\)\([0-9.]*\).*/\2/p')
[[ -z  $javaversion  ]] && javaversion=$(echo $mvnversion | sed -n 's/.*\([Oo]pen[Jj][Dd][Kk] version[[:space:]:"]*\)\([0-9.]*\).*/\2/p')

if [[ $javaversion = $'1.'* ]]; then
    # we're dealing with Java < 9                                                                                                                                                                     
    javaversion=${javaversion:2:1}
else
    # Doesn't start with 1. ?  Must be version 9 or above                                                                                                                                             
    javaversion=${javaversion%%.*}
fi

# Now that we have decided what java version maven is using, set the appropriate values:                                                                                                              
if (( $javaversion < 9 )); then
    # java versions < 9 are ... 1.6, 1.7, 1.8                                                                                                                                                         
    export SBT_EXTRAS=""
else
    # Java 9 and higher needs this:                                                                                                                                                                   
    export SBT_EXTRAS="--add-modules java.xml.bind"
fi
    

May 2018