Skip to content

Commit 2c8c2a7

Browse files
authored
Merge pull request #616 from graalvm/fix-jdk-graalvm-error-message
Fix error message when the JDK is not a GraalVM
2 parents f5df203 + 81fe799 commit 2c8c2a7

File tree

2 files changed

+35
-38
lines changed

2 files changed

+35
-38
lines changed

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageExecutableLocator.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,29 @@ public static File findNativeImageExecutable(Property<JavaLauncher> javaLauncher
9191
executablePath = metadata.getInstallationPath().file("bin/" + NATIVE_IMAGE_EXE).getAsFile();
9292
}
9393

94-
try {
95-
if (!executablePath.exists()) {
96-
logger.log("Native Image executable wasn't found. We will now try to download it. ");
97-
File graalVmHomeGuess = executablePath.getParentFile();
98-
99-
File guPath = graalVmHomeGuess.toPath().resolve(GU_EXE).toFile();
100-
if (!guPath.exists()) {
101-
throw new GradleException("'" + GU_EXE + "' at '" + guPath + "' tool wasn't found. This probably means that JDK at isn't a GraalVM distribution.");
102-
}
103-
ExecResult res = execOperations.exec(spec -> {
104-
spec.args("install", "native-image");
105-
spec.setExecutable(Paths.get(graalVmHomeGuess.getAbsolutePath(), GU_EXE));
106-
});
107-
if (res.getExitValue() != 0) {
108-
throw new GradleException("Native Image executable wasn't found, and '" + GU_EXE + "' tool failed to install it.");
109-
}
110-
diagnostics.withGuInstall();
94+
File graalVmHomeGuess = executablePath.getParentFile();
95+
File guPath = graalVmHomeGuess.toPath().resolve(GU_EXE).toFile();
96+
if (guPath.exists() && !executablePath.exists()) {
97+
logger.log("Native Image executable wasn't found. We will now try to download it. ");
98+
99+
ExecResult res = execOperations.exec(spec -> {
100+
spec.args("install", "native-image");
101+
spec.setExecutable(Paths.get(graalVmHomeGuess.getAbsolutePath(), GU_EXE));
102+
});
103+
if (res.getExitValue() != 0) {
104+
throw new GradleException("Native Image executable wasn't found, and '" + GU_EXE + "' tool failed to install it.\n" +
105+
"Make sure to declare the GRAALVM_HOME or JAVA_HOME environment variable or install GraalVM with " +
106+
"native-image in a standard location recognized by Gradle Java toolchain support");
111107
}
112-
} catch (GradleException e) {
113-
throw new GradleException("Determining GraalVM installation failed with message: " + e.getMessage() + "\n\n"
114-
+ "Make sure to declare the GRAALVM_HOME environment variable or install GraalVM with " +
115-
"native-image in a standard location recognized by Gradle Java toolchain support");
108+
diagnostics.withGuInstall();
116109
}
110+
111+
if (!executablePath.exists()) {
112+
throw new GradleException(executablePath + " wasn't found. This probably means that JDK isn't a GraalVM distribution.\n" +
113+
"Make sure to declare the GRAALVM_HOME or JAVA_HOME environment variable or install GraalVM with" +
114+
"native-image in a standard location recognized by Gradle Java toolchain support");
115+
}
116+
117117
diagnostics.withExecutablePath(executablePath);
118118
return executablePath;
119119
}

native-maven-plugin/src/main/java/org/graalvm/buildtools/utils/NativeImageConfigurationUtils.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,31 @@ public static Path getJavaHomeNativeImage(String javaHomeVariable, Boolean failF
6969

7070
Path graalHomePath = Paths.get(graalHome);
7171
Path nativeImageExe = graalHomePath.resolve("bin").resolve(NATIVE_IMAGE_EXE);
72+
Path guExe = graalHomePath.resolve("bin").resolve(GU_EXE);
7273

73-
if (!Files.exists(nativeImageExe)) {
74-
Path guExe = graalHomePath.resolve("bin").resolve(GU_EXE);
75-
if (Files.exists(guExe)) {
76-
ProcessBuilder processBuilder = new ProcessBuilder(guExe.toString(), "install", "native-image");
77-
processBuilder.inheritIO();
78-
try {
79-
Process nativeImageFetchingProcess = processBuilder.start();
80-
if (nativeImageFetchingProcess.waitFor() != 0) {
81-
throw new MojoExecutionException("native-image was not found, and '" + GU_EXE + "' tool failed to install it.");
82-
}
83-
} catch (MojoExecutionException | IOException | InterruptedException e) {
84-
throw new MojoExecutionException("Determining GraalVM installation failed with message: " + e.getMessage());
74+
if (Files.exists(guExe) && !Files.exists(nativeImageExe)) {
75+
ProcessBuilder processBuilder = new ProcessBuilder(guExe.toString(), "install", "native-image");
76+
processBuilder.inheritIO();
77+
try {
78+
Process nativeImageFetchingProcess = processBuilder.start();
79+
if (nativeImageFetchingProcess.waitFor() != 0) {
80+
throw new MojoExecutionException("native-image was not found, and '" + GU_EXE + "' tool failed to install it.");
8581
}
86-
} else if (failFast) {
87-
throw new MojoExecutionException("'" + GU_EXE + "' tool was not found in your " + javaHomeVariable + "." +
88-
"This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution.");
82+
} catch (MojoExecutionException | IOException | InterruptedException e) {
83+
throw new MojoExecutionException("Determining GraalVM installation failed with message: " + e.getMessage());
8984
}
9085
}
9186

9287
if (!Files.exists(nativeImageExe)) {
9388
if (failFast) {
94-
throw new RuntimeException("native-image is not installed in your " + javaHomeVariable + "." +
95-
"This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution.");
89+
throw new MojoExecutionException("native-image is not installed in your " + javaHomeVariable + "." +
90+
"This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution. " +
91+
"The GraalVM Native Maven Plugin requires GRAALVM_HOME or JAVA_HOME to be a GraalVM distribution.");
9692
} else {
9793
return null;
9894
}
9995
}
96+
10097
logger.info("Found GraalVM installation from " + javaHomeVariable + " variable.");
10198
return nativeImageExe;
10299
}

0 commit comments

Comments
 (0)