-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--jvm tries to find Java executable system-wide. (#11500)
Fixes `--jvm` option, given to the native image. This was failing on my machine, because when given `--jvm` option, the runner was trying to find the `java` executable from the distribution manager's runtime (on my system located in `~/.local/share/enso/runtime`) and it used the first runtime found. But the first runtime on my system is JDK 17. The `--jvm` option now tries to: - Find a JDK from the distribution manager that has the same version as the JDK used for building the engine. - If there is not an exact version match, it tries to find a runtime from distribution manager that is *newer*. - If none, fallback to system-wide search - System-wide search tries to find `java` from `$JAVA_HOME` and from `$PATH`. But this is just a fallback. # Important Notes - Added test to Engine CI jobs that pass `--jvm` argument to a native image of engine-runner - ea3af5f - `runtime-version-manager` sbt project migrated to a JPMS module - `engine-runner` now depends on `runtime-version-manager`. - Removed unnecessary stuff in `runtime-version-manager` dealing with outdated `gu` Graal Updater utility. - Extracted [GraalVersionManager](https://github.com/enso-org/enso/blob/1455b025cb8e5141409ea96b75577e8884265a70/lib/scala/runtime-version-manager/src/main/java/org/enso/runtimeversionmanager/components/GraalVersionManager.java) from [RuntimeVersionManager](https://github.com/enso-org/enso/blob/d2e8994700fd36228f7873f040668013e643190a/lib/scala/runtime-version-manager/src/main/scala/org/enso/runtimeversionmanager/components/RuntimeVersionManager.scala)
- Loading branch information
Showing
42 changed files
with
453 additions
and
889 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
distribution/launcher/THIRD-PARTY/com.typesafe.akka.akka-http-core_2.13-10.2.10/NOTICES
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
distribution/launcher/THIRD-PARTY/com.typesafe.akka.akka-http_2.13-10.2.10/NOTICES
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
distribution/launcher/THIRD-PARTY/com.typesafe.akka.akka-parsing_2.13-10.2.10/NOTICES
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
engine/runner/src/main/java/org/enso/runner/JavaFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package org.enso.runner; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Comparator; | ||
import java.util.concurrent.TimeUnit; | ||
import org.enso.distribution.DistributionManager; | ||
import org.enso.distribution.Environment; | ||
import org.enso.runtimeversionmanager.components.GraalRuntime; | ||
import org.enso.runtimeversionmanager.components.GraalVMVersion; | ||
import org.enso.runtimeversionmanager.components.GraalVersionManager; | ||
import org.enso.version.BuildVersion; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Utility class that tries to find installed JDK on the system. */ | ||
final class JavaFinder { | ||
private static final Logger logger = LoggerFactory.getLogger(JavaFinder.class); | ||
|
||
private JavaFinder() {} | ||
|
||
/** | ||
* Tries to find {@code java} executable on the system. If a system-wide JDK is not found, tries | ||
* to find it in the {@link DistributionManager distribution} runtimes. | ||
* | ||
* @return null if cannot be found. Otherwise, returns the absolute path to the executable, or | ||
* simply {@code java} if it is on the {@code PATH}. | ||
*/ | ||
static String findJavaExecutable() { | ||
var javaInRuntime = findJavaExecutableInDistributionRuntimes(); | ||
if (javaInRuntime != null) { | ||
return javaInRuntime.toAbsolutePath().toString(); | ||
} | ||
logger.warn("No appropriate JDK found in the distribution runtimes. Trying system-wide JDK."); | ||
var javaHome = System.getenv("JAVA_HOME"); | ||
if (javaHome != null) { | ||
var binDir = Path.of(javaHome).resolve("bin"); | ||
Path javaExe; | ||
if (isOnWindows()) { | ||
javaExe = binDir.resolve("java.exe"); | ||
} else { | ||
javaExe = binDir.resolve("java"); | ||
} | ||
if (javaExe.toFile().exists()) { | ||
logger.warn("Found JDK in JAVA_HOME: {}", javaHome); | ||
return javaExe.toAbsolutePath().toString(); | ||
} | ||
} | ||
logger.warn("No JDK found in JAVA_HOME. Trying java on PATH."); | ||
if (isJavaOnPath()) { | ||
var javaExe = isOnWindows() ? "java.exe" : "java"; | ||
logger.warn("Falling back to java on PATH: {}", javaExe); | ||
return javaExe; | ||
} | ||
logger.warn("No JDK found on PATH. Cannot start the runtime."); | ||
return null; | ||
} | ||
|
||
private static boolean isOnWindows() { | ||
return System.getProperty("os.name").equals("windows"); | ||
} | ||
|
||
/** | ||
* Tries to find {@code java} executable in the distribution runtime with the same version that | ||
* was used for building, or a newer one. | ||
* | ||
* @return null if not found. | ||
*/ | ||
private static Path findJavaExecutableInDistributionRuntimes() { | ||
var env = new Environment() {}; | ||
var distributionManager = new DistributionManager(env); | ||
var graalVersionManager = new GraalVersionManager(distributionManager, env); | ||
var versionUsedForBuild = | ||
new GraalVMVersion(BuildVersion.graalVersion(), BuildVersion.javaVersion()); | ||
var runtimeWithExactVersionMatch = graalVersionManager.findGraalRuntime(versionUsedForBuild); | ||
if (runtimeWithExactVersionMatch != null) { | ||
return runtimeWithExactVersionMatch.javaExecutable(); | ||
} | ||
// Try to find newer runtime (JDK). | ||
var newerRuntime = | ||
graalVersionManager.getAllRuntimes().stream() | ||
.sorted(Comparator.comparing(GraalRuntime::version)) | ||
.filter(runtime -> runtime.version().compareTo(versionUsedForBuild) > 0) | ||
.findFirst(); | ||
if (newerRuntime.isPresent()) { | ||
logger.warn( | ||
"Found newer JDK [{}] than the one used for build [{}]", | ||
newerRuntime.get().version(), | ||
versionUsedForBuild); | ||
return newerRuntime.get().javaExecutable(); | ||
} | ||
return null; | ||
} | ||
|
||
private static boolean isJavaOnPath() { | ||
try { | ||
ProcessBuilder processBuilder; | ||
if (isOnWindows()) { | ||
processBuilder = new ProcessBuilder("java.exe", "-h"); | ||
} else { | ||
processBuilder = new ProcessBuilder("java", "-h"); | ||
} | ||
Process process = processBuilder.start(); | ||
boolean exitSucc = process.waitFor(5L, TimeUnit.SECONDS); | ||
return exitSucc; | ||
} catch (IOException | InterruptedException e) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.