Skip to content

Commit

Permalink
Fixup apache#716 Calculate java home from java command
Browse files Browse the repository at this point in the history
* Add a test
* Simplify the stream processing
  • Loading branch information
ppalaga committed Oct 18, 2022
1 parent e50eedf commit d0b06ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Path javaHome() {
.orEnvironmentVariable()
.or(new ValueSource(
description -> description.append("java command"),
this::javaHomeFromPath))
DaemonParameters::javaHomeFromPath))
.orFail()
.asPath();
try {
Expand All @@ -150,8 +150,11 @@ public Path javaHome() {
}
}

private String javaHomeFromPath() {
final String jHome = OsUtils.findJavaHomeFromPath();
private static String javaHomeFromPath() {
LOG.warn("Falling back to finding JAVA_HOME by running java executable available in PATH."
+ " You may want to avoid this time consumig task by setting JAVA_HOME environment variable"
+ " or by passing java.home system property through command line or in one of mvnd configuration files.");
final String jHome = OsUtils.findJavaHomeFromJavaExecutable("java");
if (null != jHome) {
System.setProperty(Environment.JAVA_HOME.getProperty(), jHome);
}
Expand Down
25 changes: 16 additions & 9 deletions common/src/main/java/org/mvndaemon/mvnd/common/OsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,23 @@ public static long findProcessRssInKb(long pid) {
}
}

public static String findJavaHomeFromPath() {
String[] cmd = { "java", "-XshowSettings:properties", "-version" };
final List<String> output = new ArrayList<String>(1);
/**
* Executes the given {@code javaExecutable} with {@code -XshowSettings:properties -version} parameters and extracts
* the value of {@code java.home} from the output.
*
* @param javaExecutable pass {@code "java"} to get {@code java} binary available in {@code PATH} environment
* variable or pass an absolute path to a {@code "java"} executable
* @return a {@code java.home} value or null
*/
public static String findJavaHomeFromJavaExecutable(String javaExecutable) {
String[] cmd = { javaExecutable, "-XshowSettings:properties", "-version" };
final List<String> output = new ArrayList<String>();
exec(cmd, output);
List<String> javaHomeLines = output.stream().filter(l -> l.contains(" java.home = "))
.collect(Collectors.toList());
if (javaHomeLines.size() == 1) {
return javaHomeLines.get(0).trim().replaceFirst("java.home = ", "");
}
return null;
return output.stream()
.filter(l -> l.contains(" java.home = "))
.map(l -> l.substring(l.indexOf('=') + 1).trim())
.findFirst()
.orElse(null);
}

private static void exec(String[] cmd, final List<String> output) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ void kbTohumanReadable() {
Assertions.assertEquals("1t", OsUtils.kbTohumanReadable(1024 * 1024 * 1024));
}

@Test
void findJavaHomeFromPath() {
final String expectedJavaHome = System.getProperty("java.home");
Assertions.assertEquals(expectedJavaHome, OsUtils.findJavaHomeFromJavaExecutable(expectedJavaHome + "/bin/java"));
}

}

0 comments on commit d0b06ba

Please sign in to comment.