Skip to content

Commit

Permalink
Fix parseVersion to make it compatible with Termux
Browse files Browse the repository at this point in the history
  • Loading branch information
nedisy committed Jan 27, 2024
1 parent a02b813 commit 2df90e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import java.util.regex.Pattern;

public class JavaVersionParser {

private static final Pattern PATTERN = Pattern.compile(
"version \"([0-9]+\\.[0-9]+\\.[0-9_]+(?:\\.[0-9]+)*)\"");
"version \"(\\d+)[.-](\\d*)");

public int parseVersionCommand(String path) throws IOException {
Process prcs = new ProcessBuilder().command(path, "-version").start();
Expand All @@ -26,8 +27,10 @@ public int parseVersion(String output) throws IOException {
if (!matcher.find()) {
throw new IOException("Couldn't parse '" + output + "'");
}

return Integer.parseInt(getMajorVersion(matcher.group(1)));
if (matcher.group(1).equals("1")) {
return Integer.parseInt(matcher.group(2));
}
return Integer.parseInt(matcher.group(1));
}

public static String getMajorVersion(String versionString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public void testParse() throws IOException {
"Java HotSpot(TM) 64-Bit Server VM (build 25.331-b09," +
" mixed mode)");
Assertions.assertEquals(8, version);

version = parser.parseVersion(
"java version \"17-internal\" 2021-09-14\nOpenJDK" +
" Runtime Environment (build 17-internal+0-adhoc..src)\n" +
"OpenJDK 64-Bit Server VM (build 17-" +
"internal+0-adhoc..src)");
Assertions.assertEquals(17, version);

Assertions.assertThrows(IOException.class,
() -> parser.parseVersion("test"));
}
Expand Down

0 comments on commit 2df90e5

Please sign in to comment.