If you have access to a Java decompiler, you can open the SQLJDBC JAR with the decompiler and check this decompiled class: com\microsoft\sqlserver\jdbc\SQLJdbcVersion.class
It shows version info like the following:
final class SQLJdbcVersion
{
static final int major = 4;
static final int minor = 0;
static final int MMDD = 2206;
static final int revision = 100;
}
I do not know if that class is available in all versions of the SQLJDBC JAR, but I have found it in the 3.0, 4.0, and 4.2 JARs. GitHub (link below) also shows it present in versions 6 thru 8. All versions appear to include the "major" and "minor" variables; the other variables which are included differ depending on the version.
To fetch that data from the command line, perhaps you could create a script to invoke the decompiler, then extract the major and minor values from the decompiled file, and delete the decompiled file(s).
The SQL Server JDBC driver is open-source, according to this page:
https://learn.microsoft.com/en-us/sql/connect/jdbc/frequently-asked-questions-faq-for-jdbc-driver?view=sql-server-ver15
The JDBC Driver is open-source and the source code can be found on GitHub.
Therefore, there should be no legality issues in decompiling the JARs.
The current GitHub "dev" branch version of the code shows these values:
final class SQLJdbcVersion {
static final int major = 8;
static final int minor = 4;
static final int patch = 0;
static final int build = 0;
/*
* Used to load mssql-jdbc_auth DLL.
* 1. Set to "-preview" for preview release.
* 2. Set to "" (empty String) for official release.
*/
static final String releaseExt = "";
}