From df271ea9ace0fe63a3af4238fac8368b708bb7cc Mon Sep 17 00:00:00 2001 From: Paolo Di Tommaso Date: Sat, 1 Dec 2018 13:42:24 +0100 Subject: [PATCH] Fix version line on Java 10 and later #129 --- capsule/src/main/java/Capsule.java | 16 +++++++++------- capsule/src/test/java/CapsuleTest.java | 9 +++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/capsule/src/main/java/Capsule.java b/capsule/src/main/java/Capsule.java index b40cfbea..5013363b 100644 --- a/capsule/src/main/java/Capsule.java +++ b/capsule/src/main/java/Capsule.java @@ -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); } diff --git a/capsule/src/test/java/CapsuleTest.java b/capsule/src/test/java/CapsuleTest.java index ca73a42d..053bda5c 100644 --- a/capsule/src/test/java/CapsuleTest.java +++ b/capsule/src/test/java/CapsuleTest.java @@ -1571,6 +1571,15 @@ public void testPathingJar() throws Exception { } // + @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")); + } + // /////////// Utilities /////////////////////////////////// // may be called once per test (always writes jar into /capsule.jar)