Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The mission of OpenMRS is to improve health care delivery in resource-constraine

OpenMRS is a Java application which is why you need to install a Java JDK.

If you want to build the master branch you will need a Java JDK of minimum version 8.
If you want to build the master branch you will need a Java JDK of minimum version 21.

#### Maven

Expand Down
21 changes: 15 additions & 6 deletions api/src/main/java/org/openmrs/util/OpenmrsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1070,16 +1070,25 @@ public static String getOpenmrsLogLocation() {
}

/**
* Checks whether the current JVM version is at least Java 8.
* Checks whether the current JVM version is at least Java 21.
*
* @throws APIException if the current JVM version is earlier than Java 8
* @throws APIException if the current JVM version is earlier than Java 21
*/
public static void validateJavaVersion() {
// check whether the current JVM version is at least Java 8
if (System.getProperty("java.version").matches("1\\.[0-7]\\.(.*)")) {
// check whether the current JVM version is at least Java 21
String version = System.getProperty("java.version");
int majorVersion;
try {
String majorStr = version.contains(".") ? version.substring(0, version.indexOf('.')) : version;
majorVersion = Integer.parseInt(majorStr);
} catch (NumberFormatException e) {
// If we can't parse the version, skip validation
return;
Comment on lines +1082 to +1086

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Version validation bypassed for EA/pre-release Java versions due to hyphen in version string

When java.version is a pre-release string without a dot (e.g., "21-ea", "17-ea+25"), the code at line 1082 takes the else branch (version has no .), setting majorStr to the full string "21-ea". Integer.parseInt("21-ea") then throws NumberFormatException, and the catch block at line 1084-1086 silently returns, skipping validation entirely.

Root Cause and Impact

The parsing logic only splits on . but does not account for the - separator used in early-access and pre-release Java version strings (e.g., "17-ea", "21-ea+25").

For a version like "17-ea" (which should be rejected since 17 < 21), the flow is:

  1. version.contains(".")false
  2. majorStr = version"17-ea"
  3. Integer.parseInt("17-ea") → throws NumberFormatException
  4. Catch block returns without throwing → validation skipped

This means an unsupported Java version like 17-ea would pass the check, potentially leading to runtime failures later. The fix should strip the pre-release suffix before parsing, e.g.:

String majorStr = version.split("[.\\-+]"​)[0];
Suggested change
String majorStr = version.contains(".") ? version.substring(0, version.indexOf('.')) : version;
majorVersion = Integer.parseInt(majorStr);
} catch (NumberFormatException e) {
// If we can't parse the version, skip validation
return;
String majorStr = version.split("[.\\-+]")[0];
majorVersion = Integer.parseInt(majorStr);
} catch (NumberFormatException e) {
// If we can't parse the version, skip validation
return;
}
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

}
if (majorVersion < 21) {
throw new APIException(
"OpenMRS " + OpenmrsConstants.OPENMRS_VERSION_SHORT + " requires Java 8 and above, but is running under " +
System.getProperty("java.version"));
"OpenMRS " + OpenmrsConstants.OPENMRS_VERSION_SHORT + " requires Java 21 and above, but is running under " +
version);
}
}

Expand Down
31 changes: 0 additions & 31 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1168,37 +1168,6 @@
</properties>
</profile>

<!-- When using mockito-core:3.12.4 with Java 17, we get the following error.
java.lang.NullPointerException: Cannot read the array length because "this.buf" is null
mockito-core:5.6.0 does not support Java 8.
So this profile is required
-->
<profile>
<id>java17</id>
<activation>
<jdk>17</jdk>
</activation>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.19.0</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.17.7</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.17.7</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>

<profile>
<id>java21</id>
<activation>
Expand Down