Pass classpath by environment variable

This commit is contained in:
Glavo 2021-10-15 20:58:30 +08:00 committed by Yuhui Huang
parent 284ffb7aa0
commit 12368375cf
4 changed files with 45 additions and 3 deletions

View File

@ -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();

View File

@ -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<String> rawCommandLine = generateCommandLine(nativeFolder).asList();
List<String> 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;

View File

@ -146,6 +146,10 @@ public final class CommandBuilder {
return raw.stream().map(i -> i.arg).collect(Collectors.toList());
}
public List<String> asMutableList() {
return raw.stream().map(i -> i.arg).collect(Collectors.toCollection(ArrayList::new));
}
private static class Item {
String arg;
boolean parse;

View File

@ -31,6 +31,7 @@ public class ManagedProcess {
private final Process process;
private final List<String> commands;
private final String classpath;
private final Map<String, Object> properties = new HashMap<>();
private final Queue<String> lines = new ConcurrentLinkedQueue<>();
private final List<Thread> relatedThreads = new LinkedList<>();
@ -44,6 +45,20 @@ public class ManagedProcess {
public ManagedProcess(Process process, List<String> 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<String> 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.
*/