From 09a6afdf576af19ff4159364395c1593b6d909ca Mon Sep 17 00:00:00 2001 From: Gravit Date: Wed, 17 Jun 2020 15:32:30 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=A3=D0=BB=D1=83=D1=87=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B4=D1=85=D0=BE=D0=B4=D1=8F=D1=89=D0=B5=D0=B9?= =?UTF-8?q?=20Java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../launcher/ClientLauncherWrapper.java | 140 ++++++++++++------ 1 file changed, 95 insertions(+), 45 deletions(-) diff --git a/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java b/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java index fc79f4e2..c8fb317d 100644 --- a/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java +++ b/Launcher/src/main/java/pro/gravit/launcher/ClientLauncherWrapper.java @@ -19,9 +19,50 @@ public class ClientLauncherWrapper { public static final String MAGIC_ARG = "-Djdk.attach.allowAttachSelf"; public static final String WAIT_PROCESS_PROPERTY = "launcher.waitProcess"; - public static final String NO_JAVA9_CHECK_PROPERTY = "launcher.noJava9Check"; - public static boolean noJava9check = Boolean.getBoolean(NO_JAVA9_CHECK_PROPERTY); + public static final String NO_JAVA_CHECK_PROPERTY = "launcher.noJavaCheck"; + public static boolean noJavaCheck = Boolean.getBoolean(NO_JAVA_CHECK_PROPERTY); public static boolean waitProcess = Boolean.getBoolean(WAIT_PROCESS_PROPERTY); + public static class JavaVersion + { + public final Path jvmDir; + public final int version; + public boolean enabledJavaFX; + + public JavaVersion(Path jvmDir, int version) { + this.jvmDir = jvmDir; + this.version = version; + this.enabledJavaFX = true; + } + public static JavaVersion getCurrentJavaVersion() + { + return new JavaVersion(Paths.get(System.getProperty("java.home")), JVMHelper.getVersion()); + } + public static JavaVersion getByPath(Path jvmDir) throws IOException { + Path releaseFile = jvmDir.resolve("release"); + if(!IOHelper.isFile(releaseFile)) return null; + Properties properties = new Properties(); + properties.load(IOHelper.newReader(releaseFile)); + int javaVersion = getJavaVersion(properties.getProperty("JAVA_VERSION").replaceAll("\"", "")); + JavaVersion resultJavaVersion = new JavaVersion(jvmDir, javaVersion); + if(javaVersion <= 8) + { + resultJavaVersion.enabledJavaFX = isExistExtJavaLibrary(jvmDir, "jfxrt"); + } + else + { + resultJavaVersion.enabledJavaFX = tryFindModule(jvmDir, "javafx.base") != null; + if(!resultJavaVersion.enabledJavaFX) + resultJavaVersion.enabledJavaFX = tryFindModule(jvmDir.resolve("jre"), "javafx.base") != null; + } + return resultJavaVersion; + } + public static boolean isExistExtJavaLibrary(Path jvmDir, String name) + { + Path jrePath = jvmDir.resolve("lib").resolve("ext").resolve(name.concat(".jar")); + Path jdkPath = jvmDir.resolve("jre").resolve("lib").resolve("ext").resolve(name.concat(".jar")); + return IOHelper.isFile(jrePath) || IOHelper.isFile(jdkPath); + } + } public static void main(String[] arguments) throws IOException, InterruptedException { LogHelper.printVersion("Launcher"); @@ -50,18 +91,19 @@ public static void main(String[] arguments) throws IOException, InterruptedExcep LogHelper.info("Restart Launcher with JavaAgent..."); ProcessBuilder processBuilder = new ProcessBuilder(); if (waitProcess) processBuilder.inheritIO(); - Path currentJavaDirPath = Paths.get(System.getProperty("java.home")); + + JavaVersion javaVersion = null; try { - Path nextJavaDirPath = findCorrectJava(currentJavaDirPath.getParent()); - if(nextJavaDirPath != null) - { - currentJavaDirPath = nextJavaDirPath; - } - } catch (Throwable e) - { + if(!noJavaCheck) javaVersion = findJava(); + } catch (Throwable e) { LogHelper.error(e); } - Path javaBin = IOHelper.resolveJavaBin(currentJavaDirPath); + if (javaVersion == null) + { + javaVersion = JavaVersion.getCurrentJavaVersion(); + } + + Path javaBin = IOHelper.resolveJavaBin(javaVersion.jvmDir); List args = new LinkedList<>(); args.add(javaBin.toString()); String pathLauncher = IOHelper.getCodeSource(LauncherEngine.class).toString(); @@ -72,12 +114,12 @@ public static void main(String[] arguments) throws IOException, InterruptedExcep JVMHelper.addSystemPropertyToArgs(args, DirBridge.USE_CUSTOMDIR_PROPERTY); JVMHelper.addSystemPropertyToArgs(args, DirBridge.USE_OPTDIR_PROPERTY); JVMHelper.addSystemPropertyToArgs(args, DirWatcher.IGN_OVERFLOW); - if (!noJava9check && !System.getProperty("java.version").startsWith("1.8")) { + if (javaVersion.version >= 9) { LogHelper.debug("Found Java 9+ ( %s )", System.getProperty("java.version")); String pathToFx = System.getenv("PATH_TO_FX"); Path fxPath = pathToFx == null ? null : Paths.get(pathToFx); StringBuilder builder = new StringBuilder(); - Path[] findPath = new Path[]{currentJavaDirPath, fxPath}; + Path[] findPath = new Path[]{javaVersion.jvmDir, javaVersion.jvmDir.resolve("jre"), fxPath}; tryAddModule(findPath, "javafx.base", builder); tryAddModule(findPath, "javafx.graphics", builder); tryAddModule(findPath, "javafx.fxml", builder); @@ -145,46 +187,54 @@ public static boolean tryAddModule(Path[] paths, String moduleName, StringBuilde return false; } - public static Path findCorrectJava(Path javaContainerDir) + public static JavaVersion findJavaByProgramFiles(Path path) + { + JavaVersion selectedJava = null; + File[] candidates = path.toFile().listFiles(File::isDirectory); + if(candidates == null) return null; + for(File candidate : candidates) + { + Path javaPath = candidate.toPath(); + try { + JavaVersion javaVersion = JavaVersion.getByPath(javaPath); + if(javaVersion == null || javaVersion.version < 8) continue; + if(javaVersion.enabledJavaFX && (selectedJava == null || !selectedJava.enabledJavaFX)) + { + selectedJava = javaVersion; + continue; + } + if(selectedJava != null && javaVersion.enabledJavaFX && javaVersion.version < selectedJava.version) + { + selectedJava = javaVersion; + } + } catch (IOException e) { + LogHelper.error(e); + } + } + return selectedJava; + } + + public static JavaVersion findJava() { 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) + JavaVersion result = null; + Path defaultJvmContainerDir = Paths.get(System.getProperty("java.home")).getParent(); + if(defaultJvmContainerDir.getParent().getFileName().toString().contains("x86")) //Program Files (x86) ? { - //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").replaceAll("\"", "")); - 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) + Path programFiles64 = defaultJvmContainerDir.getParent().getParent().resolve("Program Files").resolve("Java"); + if(IOHelper.isDir(programFiles64)) { - LogHelper.debug("Java candidate %s throws exception %s", candidate.toPath(), e.getClass().getName()); + result = findJavaByProgramFiles(programFiles64); } } - if(resultJavaVersion < 9) noJava9check = true; - return resultJavaPath; + if(result == null) + { + result = findJavaByProgramFiles(defaultJvmContainerDir); + } + return result; } - return null; + return null; } public static int getJavaVersion(String version)