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!

2
  • "OpenJDK 11.0.8 which is equivalent to Java 1.8" - no, those are not equivalent. 11.0.8 would be Java 11 while 1.8 would be Java 8. Commented Jul 27, 2021 at 15:32
  • Yeah if I knew which Java versions were equivalent to each other I wouldn't be asking this question haha. Commented Jul 27, 2021 at 16:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.