[FEATURE][EXPERIMENTAL] Поиск подходящей джавы для Windows

This commit is contained in:
Gravit 2020-06-08 15:45:49 +07:00
parent e7fd749c2f
commit 36786c688d
No known key found for this signature in database
GPG key ID: 061981E1E85D3216

View file

@ -14,12 +14,13 @@
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Properties;
public class ClientLauncherWrapper { public class ClientLauncherWrapper {
public static final String MAGIC_ARG = "-Djdk.attach.allowAttachSelf"; public static final String MAGIC_ARG = "-Djdk.attach.allowAttachSelf";
public static final String WAIT_PROCESS_PROPERTY = "launcher.waitProcess"; public static final String WAIT_PROCESS_PROPERTY = "launcher.waitProcess";
public static final String NO_JAVA9_CHECK_PROPERTY = "launcher.noJava9Check"; public static final String NO_JAVA9_CHECK_PROPERTY = "launcher.noJava9Check";
public static final boolean noJava9check = Boolean.getBoolean(NO_JAVA9_CHECK_PROPERTY); public static boolean noJava9check = Boolean.getBoolean(NO_JAVA9_CHECK_PROPERTY);
public static boolean waitProcess = Boolean.getBoolean(WAIT_PROCESS_PROPERTY); public static boolean waitProcess = Boolean.getBoolean(WAIT_PROCESS_PROPERTY);
public static void main(String[] arguments) throws IOException, InterruptedException { public static void main(String[] arguments) throws IOException, InterruptedException {
@ -49,7 +50,18 @@ public static void main(String[] arguments) throws IOException, InterruptedExcep
LogHelper.info("Restart Launcher with JavaAgent..."); LogHelper.info("Restart Launcher with JavaAgent...");
ProcessBuilder processBuilder = new ProcessBuilder(); ProcessBuilder processBuilder = new ProcessBuilder();
if (waitProcess) processBuilder.inheritIO(); if (waitProcess) processBuilder.inheritIO();
Path javaBin = IOHelper.resolveJavaBin(Paths.get(System.getProperty("java.home"))); Path currentJavaDirPath = Paths.get(System.getProperty("java.home"));
try {
Path nextJavaDirPath = findCorrectJava(currentJavaDirPath.getParent());
if(nextJavaDirPath != null)
{
currentJavaDirPath = nextJavaDirPath;
}
} catch (Throwable e)
{
LogHelper.error(e);
}
Path javaBin = IOHelper.resolveJavaBin(currentJavaDirPath);
List<String> args = new LinkedList<>(); List<String> args = new LinkedList<>();
args.add(javaBin.toString()); args.add(javaBin.toString());
String pathLauncher = IOHelper.getCodeSource(LauncherEngine.class).toString(); String pathLauncher = IOHelper.getCodeSource(LauncherEngine.class).toString();
@ -62,11 +74,10 @@ public static void main(String[] arguments) throws IOException, InterruptedExcep
JVMHelper.addSystemPropertyToArgs(args, DirWatcher.IGN_OVERFLOW); JVMHelper.addSystemPropertyToArgs(args, DirWatcher.IGN_OVERFLOW);
if (!noJava9check && !System.getProperty("java.version").startsWith("1.8")) { if (!noJava9check && !System.getProperty("java.version").startsWith("1.8")) {
LogHelper.debug("Found Java 9+ ( %s )", System.getProperty("java.version")); LogHelper.debug("Found Java 9+ ( %s )", System.getProperty("java.version"));
Path jvmDir = Paths.get(System.getProperty("java.home"));
String pathToFx = System.getenv("PATH_TO_FX"); String pathToFx = System.getenv("PATH_TO_FX");
Path fxPath = pathToFx == null ? null : Paths.get(pathToFx); Path fxPath = pathToFx == null ? null : Paths.get(pathToFx);
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
Path[] findPath = new Path[]{jvmDir, fxPath}; Path[] findPath = new Path[]{currentJavaDirPath, fxPath};
tryAddModule(findPath, "javafx.base", builder); tryAddModule(findPath, "javafx.base", builder);
tryAddModule(findPath, "javafx.graphics", builder); tryAddModule(findPath, "javafx.graphics", builder);
tryAddModule(findPath, "javafx.fxml", builder); tryAddModule(findPath, "javafx.fxml", builder);
@ -133,4 +144,59 @@ public static boolean tryAddModule(Path[] paths, String moduleName, StringBuilde
} }
return false; return false;
} }
public static Path findCorrectJava(Path javaContainerDir)
{
if(JVMHelper.OS_TYPE == JVMHelper.OS.MUSTDIE)
{
LogHelper.debug("Java container path: %s", javaContainerDir.toString());
File programFiles = javaContainerDir.toFile();
File[] candidates = programFiles.listFiles(File::isDirectory);
if(candidates == null) return null;
int resultJavaVersion = 0;
Path resultJavaPath = null;
for(File candidate : candidates)
{
//Try get version
LogHelper.debug("Java candidate: %s", candidate.toPath().toString());
try {
Path releaseFile = candidate.toPath().resolve("release");
if(!IOHelper.isFile(releaseFile)) continue;
Properties properties = new Properties();
properties.load(IOHelper.newReader(releaseFile));
int javaVersion = getJavaVersion(properties.getProperty("JAVA_VERSION"));
if(javaVersion >= 8 && (resultJavaVersion == 0 || javaVersion < resultJavaVersion))
{
if(javaVersion > 8)
{
//Try check correct javafx
Path baseJavaFx = tryFindModule(candidate.toPath(), "javafx.base");
if(baseJavaFx == null) continue;
}
resultJavaVersion = javaVersion;
resultJavaPath = candidate.toPath();
}
} catch (Throwable e)
{
LogHelper.debug("Java candidate %s throws exception %s", candidate.toPath(), e.getClass().getName());
}
}
if(resultJavaVersion < 9) noJava9check = true;
return resultJavaPath;
}
return null;
}
public static int getJavaVersion(String version)
{
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if (dot != -1) {
version = version.substring(0, dot);
}
}
return Integer.parseInt(version);
}
} }