0

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!

1 Answers1

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Just a simple program that prints `System.getProperty("java.version")` might do the trick. It might even be used to pass the required minimum version. – Thomas Jul 27 '21 at 15:38
  • Yeah, it's going to be looked at by the computer, if the user's Java version isn't a version we support, then we raise an error telling them to change/update it. I appreciate the help though, I'll look into your solution, my current problem is that I know nothing about Java, so I'm going to reiterate your solution to someone who does and see if they can help me implement it. – stackoverflowismyonlyhope Jul 27 '21 at 16:28
  • 2
    This sounds like you are shipping a product. Tell the Java guys to enclose a JRE which you KNOW works because you have TESTED it, instead of relying on the user installing something which may or may not work. Why have you been given this assignment if you do not know anything about Java? – Thorbjørn Ravn Andersen Jul 27 '21 at 16:38
  • Haha, I know Python pretty well, and the product we're "shipping" is an open source Python wrapper for an open source Java program. So the majority of what I'm doing can be done in Python, we were just trying to see if we could build a Java version check for our Python side to make catching errors easier, like not letting the user compile the program if their Java version isn't at least Java 8. – stackoverflowismyonlyhope Jul 28 '21 at 16:24
  • In that case, I would personally go for running a small java program written by your team testing the JVM underneath and let the python wrapper examine that output. – Thorbjørn Ravn Andersen Jul 28 '21 at 17:52