Skip to content

Commit

Permalink
Fix error message when the JDK is not a GraalVM
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon authored and dnestoro committed Sep 3, 2024
1 parent f5df203 commit 81fe799
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,29 @@ public static File findNativeImageExecutable(Property<JavaLauncher> javaLauncher
executablePath = metadata.getInstallationPath().file("bin/" + NATIVE_IMAGE_EXE).getAsFile();
}

try {
if (!executablePath.exists()) {
logger.log("Native Image executable wasn't found. We will now try to download it. ");
File graalVmHomeGuess = executablePath.getParentFile();

File guPath = graalVmHomeGuess.toPath().resolve(GU_EXE).toFile();
if (!guPath.exists()) {
throw new GradleException("'" + GU_EXE + "' at '" + guPath + "' tool wasn't found. This probably means that JDK at isn't a GraalVM distribution.");
}
ExecResult res = execOperations.exec(spec -> {
spec.args("install", "native-image");
spec.setExecutable(Paths.get(graalVmHomeGuess.getAbsolutePath(), GU_EXE));
});
if (res.getExitValue() != 0) {
throw new GradleException("Native Image executable wasn't found, and '" + GU_EXE + "' tool failed to install it.");
}
diagnostics.withGuInstall();
File graalVmHomeGuess = executablePath.getParentFile();
File guPath = graalVmHomeGuess.toPath().resolve(GU_EXE).toFile();
if (guPath.exists() && !executablePath.exists()) {
logger.log("Native Image executable wasn't found. We will now try to download it. ");

ExecResult res = execOperations.exec(spec -> {
spec.args("install", "native-image");
spec.setExecutable(Paths.get(graalVmHomeGuess.getAbsolutePath(), GU_EXE));
});
if (res.getExitValue() != 0) {
throw new GradleException("Native Image executable wasn't found, and '" + GU_EXE + "' tool failed to install it.\n" +
"Make sure to declare the GRAALVM_HOME or JAVA_HOME environment variable or install GraalVM with " +
"native-image in a standard location recognized by Gradle Java toolchain support");
}
} catch (GradleException e) {
throw new GradleException("Determining GraalVM installation failed with message: " + e.getMessage() + "\n\n"
+ "Make sure to declare the GRAALVM_HOME environment variable or install GraalVM with " +
"native-image in a standard location recognized by Gradle Java toolchain support");
diagnostics.withGuInstall();
}

if (!executablePath.exists()) {
throw new GradleException(executablePath + " wasn't found. This probably means that JDK isn't a GraalVM distribution.\n" +
"Make sure to declare the GRAALVM_HOME or JAVA_HOME environment variable or install GraalVM with" +
"native-image in a standard location recognized by Gradle Java toolchain support");
}

diagnostics.withExecutablePath(executablePath);
return executablePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,31 @@ public static Path getJavaHomeNativeImage(String javaHomeVariable, Boolean failF

Path graalHomePath = Paths.get(graalHome);
Path nativeImageExe = graalHomePath.resolve("bin").resolve(NATIVE_IMAGE_EXE);
Path guExe = graalHomePath.resolve("bin").resolve(GU_EXE);

if (!Files.exists(nativeImageExe)) {
Path guExe = graalHomePath.resolve("bin").resolve(GU_EXE);
if (Files.exists(guExe)) {
ProcessBuilder processBuilder = new ProcessBuilder(guExe.toString(), "install", "native-image");
processBuilder.inheritIO();
try {
Process nativeImageFetchingProcess = processBuilder.start();
if (nativeImageFetchingProcess.waitFor() != 0) {
throw new MojoExecutionException("native-image was not found, and '" + GU_EXE + "' tool failed to install it.");
}
} catch (MojoExecutionException | IOException | InterruptedException e) {
throw new MojoExecutionException("Determining GraalVM installation failed with message: " + e.getMessage());
if (Files.exists(guExe) && !Files.exists(nativeImageExe)) {
ProcessBuilder processBuilder = new ProcessBuilder(guExe.toString(), "install", "native-image");
processBuilder.inheritIO();
try {
Process nativeImageFetchingProcess = processBuilder.start();
if (nativeImageFetchingProcess.waitFor() != 0) {
throw new MojoExecutionException("native-image was not found, and '" + GU_EXE + "' tool failed to install it.");
}
} else if (failFast) {
throw new MojoExecutionException("'" + GU_EXE + "' tool was not found in your " + javaHomeVariable + "." +
"This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution.");
} catch (MojoExecutionException | IOException | InterruptedException e) {
throw new MojoExecutionException("Determining GraalVM installation failed with message: " + e.getMessage());
}
}

if (!Files.exists(nativeImageExe)) {
if (failFast) {
throw new RuntimeException("native-image is not installed in your " + javaHomeVariable + "." +
"This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution.");
throw new MojoExecutionException("native-image is not installed in your " + javaHomeVariable + "." +
"This probably means that the JDK at '" + graalHomePath + "' is not a GraalVM distribution. " +
"The GraalVM Native Maven Plugin requires GRAALVM_HOME or JAVA_HOME to be a GraalVM distribution.");
} else {
return null;
}
}

logger.info("Found GraalVM installation from " + javaHomeVariable + " variable.");
return nativeImageExe;
}
Expand Down

0 comments on commit 81fe799

Please sign in to comment.