Class JavaService
- Namespace
- McServerLauncher.Services
- Assembly
- McServerLauncher.dll
Detects the machine's Java installations and, if needed, downloads the right version (Adoptium Temurin) for a specific Minecraft version. Works on Windows and Linux.
public class JavaService
- Inheritance
-
JavaService
- Inherited Members
Methods
AcceptsFlags(string, IReadOnlyList<string>)
Whether this Java starts with flags: java <flags> -version
exits cleanly.
public bool AcceptsFlags(string javaExe, IReadOnlyList<string> flags)
Parameters
javaExestringflagsIReadOnlyList<string>
Returns
Remarks
A JVM refuses to start at all over an option it does not know, or one that needs an unlock flag it was not given. The memory profile only uses options that are final in the versions it gives them to, but a vendor build can still differ, and the price of being wrong is a server that does not start. Asking costs one short JVM run per Java and set of flags; the answer is remembered against the file like the version is, and for a bare "java" for as long as the app runs.
DetectInstalled()
Searches for the java executable in common locations and returns their versions.
public List<JavaService.JavaInstall> DetectInstalled()
Returns
EnsureJavaAsync(int, IProgress<string>?, CancellationToken)
Returns the path to a java executable compatible with the required version. If none is installed, downloads and installs the matching Temurin. Throws if it can't.
public Task<string> EnsureJavaAsync(int requiredMajor, IProgress<string>? log, CancellationToken ct = default)
Parameters
requiredMajorintlogIProgress<string>ctCancellationToken
Returns
GetGameVersionFromJar(string)
Reads the Minecraft version a server.jar belongs to (modern vanilla jars include it in version.json as "id"). Returns null if it can't be determined.
public string? GetGameVersionFromJar(string jarPath)
Parameters
jarPathstring
Returns
GetMajorVersion(string)
The major version (8, 17, 21, 25...) of a Java executable. 0 on failure.
public int GetMajorVersion(string javaExe)
Parameters
javaExestring
Returns
GetRequiredJavaFromForgeLibraries(string, string?)
Reads the required Java for a modern Forge server, which has no runnable server.jar of its
own (it launches through an @args-file, so GetRequiredJavaFromJar(string) on the
configured jar path always fails): the Forge installer keeps the vanilla server jar under
"libraries/net/minecraft/server/<version>/", and that jar carries the usual
version.json. Prefers the folder matching gameVersion (a leftover from a
previous Minecraft version could linger after an upgrade), then tries any other. Returns
null if no readable jar is found.
public int? GetRequiredJavaFromForgeLibraries(string serverFolder, string? gameVersion)
Parameters
Returns
- int?
GetRequiredJavaFromJar(string)
Reads the Java a server.jar needs (modern versions include it in version.json). Returns null if it can't be determined (very old servers or non-standard jars).
public int? GetRequiredJavaFromJar(string jarPath)
Parameters
jarPathstring
Returns
- int?
GetRuntime(string)
The version, VM and word size of a Java executable. Unknown on failure.
public JavaService.JavaRuntime GetRuntime(string javaExe)
Parameters
javaExestring
Returns
Remarks
Asking means starting a JVM, a fraction of a second to more than one on a slow disk, and it was asked twice on every start — once to check the Java and again to build the command line — on the UI thread, and once per JDK found when a server was created. The answer only changes when the file does, so it is remembered against the file's size and write time; a Java updated in place is asked again.
A bare "java" resolved through the PATH has no file to stamp and is not remembered.
IsCompatible(int, int)
An installed Java version is valid for the required one (exact, or newer if 17+).
public static bool IsCompatible(int installed, int required)
Parameters
Returns
Remarks
One exception, for Java 16 — what Mojang declares for Minecraft 1.17 and 1.17.1. Adoptium publishes no Java 16 at all, so if 17 did not count, a 1.17 server could neither use the Java 17 already on the machine nor download a 16, and would not start without a Java installed by hand. 17 runs those versions; anything later is not promised to.