From 12368375cf31b5c360c204bfaf6a68431703d5ca Mon Sep 17 00:00:00 2001 From: Glavo Date: Fri, 15 Oct 2021 20:58:30 +0800 Subject: [PATCH] Pass classpath by environment variable --- .../jackhuang/hmcl/game/LauncherHelper.java | 5 ++++ .../hmcl/launch/DefaultLauncher.java | 15 +++++++++--- .../hmcl/util/platform/CommandBuilder.java | 4 ++++ .../hmcl/util/platform/ManagedProcess.java | 24 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java index fbf5dbe26..f83fc8f8c 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java @@ -621,6 +621,11 @@ public final class LauncherHelper { LOG.info("Launched process: " + command); + String classpath = process.getClasspath(); + if (classpath != null) { + LOG.info("Process CLASSPATH: " + classpath); + } + if (showLogs) Platform.runLater(() -> { logWindow = new LogWindow(); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java index d7113ef83..c464806bc 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java @@ -354,7 +354,7 @@ public class DefaultLauncher extends Launcher { @Override public ManagedProcess launch() throws IOException, InterruptedException { - File nativeFolder = null; + File nativeFolder; if (options.getNativesDirType() == NativesDirectoryType.VERSION_FOLDER) { nativeFolder = repository.getNativeDirectory(version.getId(), options.getJava().getPlatform()); } else { @@ -362,7 +362,15 @@ public class DefaultLauncher extends Launcher { } // To guarantee that when failed to generate launch command line, we will not call pre-launch command - List rawCommandLine = generateCommandLine(nativeFolder).asList(); + List rawCommandLine = generateCommandLine(nativeFolder).asMutableList(); + + // Pass classpath using the environment variable, to reduce the command length + String classpath = null; + final int cpIndex = rawCommandLine.indexOf("-cp"); + if (cpIndex >= 0 && cpIndex < rawCommandLine.size() - 1) { + rawCommandLine.remove(cpIndex); // remove "-cp" + classpath = rawCommandLine.remove(cpIndex); + } if (rawCommandLine.stream().anyMatch(StringUtils::isBlank)) { throw new IllegalStateException("Illegal command line " + rawCommandLine); @@ -388,13 +396,14 @@ public class DefaultLauncher extends Launcher { } String appdata = options.getGameDir().getAbsoluteFile().getParent(); if (appdata != null) builder.environment().put("APPDATA", appdata); + if (classpath != null) builder.environment().put("CLASSPATH", classpath); builder.environment().putAll(getEnvVars()); process = builder.start(); } catch (IOException e) { throw new ProcessCreationException(e); } - ManagedProcess p = new ManagedProcess(process, rawCommandLine); + ManagedProcess p = new ManagedProcess(process, rawCommandLine, classpath); if (listener != null) startMonitors(p, listener, daemon); return p; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/CommandBuilder.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/CommandBuilder.java index 3eec70312..5f18dc4e7 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/CommandBuilder.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/CommandBuilder.java @@ -146,6 +146,10 @@ public final class CommandBuilder { return raw.stream().map(i -> i.arg).collect(Collectors.toList()); } + public List asMutableList() { + return raw.stream().map(i -> i.arg).collect(Collectors.toCollection(ArrayList::new)); + } + private static class Item { String arg; boolean parse; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java index d3ee492fd..f4841d2a0 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/ManagedProcess.java @@ -31,6 +31,7 @@ public class ManagedProcess { private final Process process; private final List commands; + private final String classpath; private final Map properties = new HashMap<>(); private final Queue lines = new ConcurrentLinkedQueue<>(); private final List relatedThreads = new LinkedList<>(); @@ -44,6 +45,20 @@ public class ManagedProcess { public ManagedProcess(Process process, List commands) { this.process = process; this.commands = Collections.unmodifiableList(new ArrayList<>(commands)); + this.classpath = null; + } + + /** + * Constructor. + * + * @param process the raw system process that this instance manages. + * @param commands the command line of {@code process}. + * @param classpath the classpath of java process + */ + public ManagedProcess(Process process, List commands, String classpath) { + this.process = process; + this.commands = Collections.unmodifiableList(new ArrayList<>(commands)); + this.classpath = classpath; } /** @@ -64,6 +79,15 @@ public class ManagedProcess { return commands; } + /** + * The classpath. + * + * @return classpath + */ + public String getClasspath() { + return classpath; + } + /** * To save some information you need. */