Skip to content

Commit 6170718

Browse files
committed
fix #420: fix detection of java version when JAVA_TOOL_OPTIONS is set
1 parent 58d8960 commit 6170718

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ bin
66
.idea
77
*.iml
88
.java-version
9+
*.bak

plexus-compilers/plexus-compiler-javac/src/main/java/org/codehaus/plexus/compiler/javac/JavacCompiler.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,17 @@ private String getOutOfProcessJavacVersion(String executable) throws CompilerExc
259259
*/
260260
cli.addArguments(new String[] {"-version"}); //
261261
CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
262+
CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
262263
try {
263-
int exitCode = CommandLineUtils.executeCommandLine(cli, out, out);
264+
int exitCode = CommandLineUtils.executeCommandLine(cli, out, err);
264265
if (exitCode != 0) {
265266
throw new CompilerException("Could not retrieve version from " + executable + ". Exit code "
266-
+ exitCode + ", Output: " + out.getOutput());
267+
+ exitCode + ", Output: " + out.getOutput() + ", Error: " + err.getOutput());
267268
}
268269
} catch (CommandLineException e) {
269270
throw new CompilerException("Error while executing the external compiler " + executable, e);
270271
}
271-
version = extractMajorAndMinorVersion(out.getOutput());
272+
version = tryParseVersion(out.getOutput().trim());
272273
VERSION_PER_EXECUTABLE.put(executable, version);
273274
}
274275
return version;
@@ -282,6 +283,19 @@ static String extractMajorAndMinorVersion(String text) {
282283
return matcher.group();
283284
}
284285

286+
private String tryParseVersion(String version) {
287+
if (version.startsWith("javac ")) {
288+
version = version.substring(6);
289+
if (version.startsWith("1.")) {
290+
version = version.substring(0, 3);
291+
} else {
292+
version = version.substring(0, 2);
293+
}
294+
return version;
295+
}
296+
return null;
297+
}
298+
285299
@Override
286300
public CompilerResult performCompile(CompilerConfiguration config) throws CompilerException {
287301
File destinationDir = new File(config.getOutputLocation());

plexus-compilers/plexus-compiler-javac/src/test/java/org/codehaus/plexus/compiler/javac/JavacCompilerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,6 @@ void testExtractMajorAndMinorVersion() {
121121
assertEquals("11.0", JavacCompiler.extractMajorAndMinorVersion("javac 11.0.22"));
122122
assertEquals("11.0", JavacCompiler.extractMajorAndMinorVersion("11.0.22"));
123123
assertEquals("21", JavacCompiler.extractMajorAndMinorVersion("javac 21"));
124+
assertEquals("1.8", JavacCompiler.extractMajorAndMinorVersion("javac 1.8.0_432"));
124125
}
125126
}

0 commit comments

Comments
 (0)