Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix version line on Java 10 and later #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
16 changes: 9 additions & 7 deletions capsule/src/main/java/Capsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4160,17 +4160,19 @@ private static Path getJavaExecutable0(Path javaHome) {
return javaHome.resolve("bin").resolve(exec + (isWindows() ? ".exe" : ""));
}

private static final Pattern PAT_JAVA_VERSION_LINE = Pattern.compile(".*?\"(.+?)\"");
private static final Pattern PAT_JAVA_VERSION_LINE = Pattern.compile(".*?\"(.+?)\".*");

static String parseJavaVersionLine(String versionLine) {
final Matcher m = PAT_JAVA_VERSION_LINE.matcher(versionLine);
if (!m.matches())
throw new IllegalArgumentException("Could not parse version line: " + versionLine);
return m.group(1);
}

private static String getActualJavaVersion(Path javaHome) {
try {
final String versionLine = first(exec(1, true, new ProcessBuilder(asList(getJavaExecutable0(javaHome).toString(), "-version"))));
final Matcher m = PAT_JAVA_VERSION_LINE.matcher(versionLine);
if (!m.matches())
throw new IllegalArgumentException("Could not parse version line: " + versionLine);
final String version = m.group(1);

return version;
return parseJavaVersionLine(versionLine);
} catch (Exception e) {
throw rethrow(e);
}
Expand Down
9 changes: 9 additions & 0 deletions capsule/src/test/java/CapsuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,15 @@ public void testPathingJar() throws Exception {
}
//</editor-fold>

@Test
public void testParseJavaVersionLine() {
assertEquals("1.8.0_161", Capsule.parseJavaVersionLine("java version \"1.8.0_161\""));
assertEquals("9.0.4", Capsule.parseJavaVersionLine("java version \"9.0.4\""));
assertEquals("10", Capsule.parseJavaVersionLine("java version \"10\" 2018-03-20"));
assertEquals("11", Capsule.parseJavaVersionLine("java version \"11\" 2018-09-25"));
assertEquals("10.0.2", Capsule.parseJavaVersionLine("openjdk version \"10.0.2\" 2018-07-17"));
}

//<editor-fold defaultstate="collapsed" desc="Utilities">
/////////// Utilities ///////////////////////////////////
// may be called once per test (always writes jar into /capsule.jar)
Expand Down