1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/* SPDX-FileCopyrightText: 2026 Project Tick
* SPDX-FileContributor: Project Tick
* SPDX-License-Identifier: GPL-3.0-or-later
*
* MeshMC - A Custom Launcher for Minecraft
* Copyright (C) 2026 Project Tick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "VerifyJavaInstall.h"
#include <QDir>
#include <QDirIterator>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QProcess>
#include <QRegularExpression>
#include <launch/LaunchTask.h>
#include <minecraft/MinecraftInstance.h>
#include <minecraft/PackProfile.h>
#include <minecraft/VersionFilterData.h>
#include "Application.h"
#include "FileSystem.h"
#include "Json.h"
#include "java/JavaUtils.h"
#ifndef MeshMC_DISABLE_JAVA_DOWNLOADER
#include "BuildConfig.h"
#include "net/Download.h"
#endif
#ifdef major
#undef major
#endif
#ifdef minor
#undef minor
#endif
namespace
{
std::optional<JavaVersion> probeJavaVersion(const QString& javaPath)
{
const auto checkerJar =
FS::PathCombine(APPLICATION->getJarsPath(), "JavaCheck.jar");
if (!QFileInfo::exists(checkerJar)) {
return std::nullopt;
}
QProcess process;
process.setProgram(javaPath);
process.setArguments({"-jar", checkerJar});
process.setProcessEnvironment(CleanEnviroment());
process.setProcessChannelMode(QProcess::SeparateChannels);
process.start();
if (!process.waitForFinished(15000) ||
process.exitStatus() != QProcess::NormalExit ||
process.exitCode() != 0) {
return std::nullopt;
}
const auto stdoutData =
QString::fromLocal8Bit(process.readAllStandardOutput());
const auto lines = stdoutData.split('\n', Qt::SkipEmptyParts);
for (const auto& rawLine : lines) {
const auto line = rawLine.trimmed();
if (!line.startsWith("java.version=")) {
continue;
}
return JavaVersion(line.mid(QString("java.version=").size()));
}
return std::nullopt;
}
} // namespace
int VerifyJavaInstall::determineRequiredJavaMajor() const
{
auto m_inst =
std::dynamic_pointer_cast<MinecraftInstance>(m_parent->instance());
auto minecraftComponent =
m_inst->getPackProfile()->getComponent("net.minecraft");
if (minecraftComponent->getReleaseDateTime() >=
g_VersionFilterData.java25BeginsDate)
return 25;
if (minecraftComponent->getReleaseDateTime() >=
g_VersionFilterData.java21BeginsDate)
return 21;
if (minecraftComponent->getReleaseDateTime() >=
g_VersionFilterData.java17BeginsDate)
return 17;
if (minecraftComponent->getReleaseDateTime() >=
g_VersionFilterData.java16BeginsDate)
return 16;
if (minecraftComponent->getReleaseDateTime() >=
g_VersionFilterData.java8BeginsDate)
return 8;
return 0;
}
QString VerifyJavaInstall::javaInstallDir() const
{
return JavaUtils::managedJavaRoot();
}
QString VerifyJavaInstall::findInstalledJava(int requiredMajor) const
{
JavaUtils javaUtils;
QList<QString> systemJavas = javaUtils.FindJavaPaths();
QSet<QString> seenPaths;
for (const QString& javaPath : systemJavas) {
QString resolved = FS::ResolveExecutable(javaPath);
if (resolved.isEmpty() || seenPaths.contains(resolved))
continue;
seenPaths.insert(resolved);
const auto version = probeJavaVersion(resolved);
if (version.has_value() && version->major() >= requiredMajor) {
return resolved;
}
}
return {};
}
void VerifyJavaInstall::executeTask()
{
auto m_inst =
std::dynamic_pointer_cast<MinecraftInstance>(m_parent->instance());
auto javaVersion = m_inst->getJavaVersion();
int requiredMajor = determineRequiredJavaMajor();
// No Java requirement or already met
if (requiredMajor == 0 || javaVersion.major() >= requiredMajor) {
emitSucceeded();
return;
}
// Java version insufficient — try to find an already-downloaded one
emit logLine(
tr("Current Java version %1 does not meet the requirement of Java %2.")
.arg(javaVersion.toString())
.arg(requiredMajor),
MessageLevel::Warning);
QString existingJava = findInstalledJava(requiredMajor);
if (!existingJava.isEmpty()) {
emit logLine(tr("Found installed Java %1 at: %2")
.arg(requiredMajor)
.arg(existingJava),
MessageLevel::MeshMC);
#ifndef MeshMC_DISABLE_JAVA_DOWNLOADER
setJavaPathAndSucceed(existingJava);
#else
m_inst->settings()->set("OverrideJavaLocation", true);
m_inst->settings()->set("JavaPath", existingJava);
emit logLine(tr("Java path set to: %1").arg(existingJava),
MessageLevel::MeshMC);
emitSucceeded();
#endif
return;
}
#ifndef MeshMC_DISABLE_JAVA_DOWNLOADER
// Not found — auto-download
emit logLine(
tr("No installed Java %1 found. Downloading...").arg(requiredMajor),
MessageLevel::MeshMC);
autoDownloadJava(requiredMajor);
#else
emitFailed(
tr("Java %1 is required but not installed. Please install it manually.")
.arg(requiredMajor));
#endif
}
#ifndef MeshMC_DISABLE_JAVA_DOWNLOADER
void VerifyJavaInstall::autoDownloadJava(int requiredMajor)
{
// Fetch version list from net.minecraft.java (Mojang)
fetchVersionList(requiredMajor);
}
void VerifyJavaInstall::fetchVersionList(int requiredMajor)
{
m_fetchData.clear();
QString uid = "net.minecraft.java";
QString url = QString("%1%2/index.json").arg(BuildConfig.META_URL, uid);
m_fetchJob = new NetJob(tr("Fetch Java versions"), APPLICATION->network());
auto dl = Net::Download::makeByteArray(QUrl(url), &m_fetchData);
m_fetchJob->addNetAction(dl);
connect(
m_fetchJob.get(), &NetJob::succeeded, this,
[this, uid, requiredMajor]() {
m_fetchJob.reset();
QJsonDocument doc;
try {
doc = Json::requireDocument(m_fetchData);
} catch (const Exception& e) {
emitFailed(
tr("Failed to parse Java version list from meta server: %1")
.arg(e.cause()));
return;
}
if (!doc.isObject()) {
emitFailed(
tr("Failed to parse Java version list from meta server."));
return;
}
auto versions = JavaDownload::parseVersionIndex(doc.object(), uid);
// Find the matching version (e.g., "java25" for requiredMajor=25)
QString targetVersionId = QString("java%1").arg(requiredMajor);
bool found = false;
for (const auto& ver : versions) {
if (ver.versionId == targetVersionId) {
found = true;
fetchRuntimes(ver.versionId, requiredMajor);
return;
}
}
if (!found) {
emitFailed(tr("Java %1 is not available for download from "
"Mojang. Please install it manually.")
.arg(requiredMajor));
}
});
connect(m_fetchJob.get(), &NetJob::failed, this,
[this, requiredMajor](QString reason) {
emitFailed(tr("Failed to fetch Java version list: %1. Please "
"install Java %2 manually.")
.arg(reason)
.arg(requiredMajor));
m_fetchJob.reset();
});
m_fetchJob->start();
}
void VerifyJavaInstall::fetchRuntimes(const QString& versionId,
int requiredMajor)
{
m_fetchData.clear();
QString uid = "net.minecraft.java";
QString url =
QString("%1%2/%3.json").arg(BuildConfig.META_URL, uid, versionId);
m_fetchJob =
new NetJob(tr("Fetch Java runtime details"), APPLICATION->network());
auto dl = Net::Download::makeByteArray(QUrl(url), &m_fetchData);
m_fetchJob->addNetAction(dl);
connect(m_fetchJob.get(), &NetJob::succeeded, this,
[this, requiredMajor]() {
auto fetchJob = std::move(m_fetchJob);
QJsonDocument doc;
try {
doc = Json::requireDocument(m_fetchData);
} catch (const Exception& e) {
emitFailed(tr("Failed to parse Java runtime details: %1")
.arg(e.cause()));
return;
}
if (!doc.isObject()) {
emitFailed(tr("Failed to parse Java runtime details."));
return;
}
auto allRuntimes = JavaDownload::parseRuntimes(doc.object());
QString myOS = JavaDownload::currentRuntimeOS();
// Filter for current platform
for (const auto& rt : allRuntimes) {
if (rt.runtimeOS == myOS) {
emit logLine(tr("Downloading %1 (%2)...")
.arg(rt.name, rt.version.toString()),
MessageLevel::MeshMC);
startDownload(rt, requiredMajor);
return;
}
}
emitFailed(tr("No Java %1 download available for your platform "
"(%2). Please install it manually.")
.arg(requiredMajor)
.arg(myOS));
});
connect(m_fetchJob.get(), &NetJob::failed, this, [this](QString reason) {
emitFailed(tr("Failed to fetch Java runtime details: %1").arg(reason));
m_fetchJob.reset();
});
m_fetchJob->start();
}
void VerifyJavaInstall::startDownload(const JavaDownload::RuntimeEntry& runtime,
int requiredMajor)
{
QString dirName =
QString("%1-%2").arg(runtime.name, runtime.version.toString());
QString targetDir =
FS::PathCombine(javaInstallDir(), runtime.vendor, dirName);
m_downloadTask =
std::make_unique<JavaDownloadTask>(runtime, targetDir, this);
connect(m_downloadTask.get(), &Task::succeeded, this, [this]() {
QString javaPath = m_downloadTask->installedJavaPath();
if (javaPath.isEmpty()) {
emitFailed(
tr("Java was downloaded but the binary could not be found."));
return;
}
emit logLine(tr("Java downloaded and installed at: %1").arg(javaPath),
MessageLevel::MeshMC);
setJavaPathAndSucceed(javaPath);
});
connect(m_downloadTask.get(), &Task::failed, this,
[this, requiredMajor](const QString& reason) {
emitFailed(tr("Failed to download Java %1: %2")
.arg(requiredMajor)
.arg(reason));
m_downloadTask.reset();
});
connect(m_downloadTask.get(), &Task::status, this,
[this](const QString& status) {
emit logLine(status, MessageLevel::MeshMC);
});
m_downloadTask->start();
}
void VerifyJavaInstall::setJavaPathAndSucceed(const QString& javaPath)
{
auto m_inst =
std::dynamic_pointer_cast<MinecraftInstance>(m_parent->instance());
// Set Java path override on the instance only, not globally
m_inst->settings()->set("OverrideJavaLocation", true);
m_inst->settings()->set("JavaPath", javaPath);
emit logLine(tr("Java path set to: %1").arg(javaPath),
MessageLevel::MeshMC);
emitSucceeded();
}
#endif
|