[FEATURE] Улучшение поиска подходящей Java

This commit is contained in:
Gravit 2020-06-17 15:32:30 +07:00
parent a65837d81d
commit 09a6afdf57
No known key found for this signature in database
GPG key ID: 061981E1E85D3216

View file

@ -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<String> 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)