Verifying Java version in Python

I'm working on a Python program that acts as a wrapper for a Java program. I am currently trying to validate the user's version of Java so that I can either say it's good, or that the user needs a different version of Java. My problem comes from the fact that there are multiple kinds of Java, for example, someone running OpenJDK has a version number pattern like 11.0.8, whereas Java will have a version number pattern like 1.8. My current code can get this number, but I can't find anything that says something like "AdoptOpenJDK 2.5 is equivalent to OpenJDK 11.0.8 which is equivalent to Java 1.8." If I could get something like that then this problem could be solved with a simple dictionary lookup.

My current code:

java_version = subprocess.check_output(['java', '-version'], stderr=subprocess.STDOUT) # returns bytestring
java_version = java_version.decode("utf-8") # convert to regular string
version_number_pattern = r'\"(\d+\.\d+).*\"' # regex pattern matching
version = re.search(version_number_pattern, java_version).groups()[0]

attained from here

Any help would be greatly appreciated, thank you!

Upvotes: 0

Views: 362

Answers (1)

The most reliable approach is running a small program testing for the capabilities you need. Compile the main class for java 1.2 so it will always be executable and then try-catch-invoke test classes.

Question is what you need it for. If it is just a human, present the string and let them visually inspect. If it is to validate the JVM run test code.

Upvotes: 1

Related Questions