2013-10-05 00:08:13 +00:00
|
|
|
/* Copyright 2013 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QString>
|
|
|
|
#include <QDir>
|
2014-01-22 17:28:56 +00:00
|
|
|
#include <QStringList>
|
2013-11-04 01:53:05 +00:00
|
|
|
|
2013-10-14 01:59:21 +00:00
|
|
|
#include <setting.h>
|
2013-11-04 01:53:05 +00:00
|
|
|
#include <pathutils.h>
|
|
|
|
|
|
|
|
#include "MultiMC.h"
|
|
|
|
|
|
|
|
#include "logger/QsLog.h"
|
2014-05-08 19:20:10 +00:00
|
|
|
#include "logic/java/JavaUtils.h"
|
|
|
|
#include "logic/java/JavaCheckerJob.h"
|
|
|
|
#include "logic/java/JavaVersionList.h"
|
2013-10-05 00:08:13 +00:00
|
|
|
|
2013-10-06 18:54:52 +00:00
|
|
|
JavaUtils::JavaUtils()
|
|
|
|
{
|
|
|
|
}
|
2013-10-05 00:08:13 +00:00
|
|
|
|
2013-12-11 03:54:39 +00:00
|
|
|
JavaVersionPtr JavaUtils::MakeJavaPtr(QString path, QString id, QString arch)
|
|
|
|
{
|
|
|
|
JavaVersionPtr javaVersion(new JavaVersion());
|
|
|
|
|
|
|
|
javaVersion->id = id;
|
|
|
|
javaVersion->arch = arch;
|
|
|
|
javaVersion->path = path;
|
|
|
|
|
|
|
|
return javaVersion;
|
|
|
|
}
|
|
|
|
|
2013-10-14 01:59:21 +00:00
|
|
|
JavaVersionPtr JavaUtils::GetDefaultJava()
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
2013-10-14 01:59:21 +00:00
|
|
|
JavaVersionPtr javaVersion(new JavaVersion());
|
2013-10-05 00:08:13 +00:00
|
|
|
|
2013-10-14 01:59:21 +00:00
|
|
|
javaVersion->id = "java";
|
|
|
|
javaVersion->arch = "unknown";
|
|
|
|
javaVersion->path = "java";
|
|
|
|
|
|
|
|
return javaVersion;
|
2013-10-05 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if WINDOWS
|
2013-10-14 01:59:21 +00:00
|
|
|
QList<JavaVersionPtr> JavaUtils::FindJavaFromRegistryKey(DWORD keyType, QString keyName)
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
2013-10-14 01:59:21 +00:00
|
|
|
QList<JavaVersionPtr> javas;
|
2013-10-06 18:54:52 +00:00
|
|
|
|
|
|
|
QString archType = "unknown";
|
2013-11-04 01:53:05 +00:00
|
|
|
if (keyType == KEY_WOW64_64KEY)
|
|
|
|
archType = "64";
|
|
|
|
else if (keyType == KEY_WOW64_32KEY)
|
|
|
|
archType = "32";
|
2013-10-05 00:08:13 +00:00
|
|
|
|
|
|
|
HKEY jreKey;
|
2013-11-04 01:53:05 +00:00
|
|
|
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyName.toStdString().c_str(), 0,
|
|
|
|
KEY_READ | keyType | KEY_ENUMERATE_SUB_KEYS, &jreKey) == ERROR_SUCCESS)
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
2013-10-06 18:54:52 +00:00
|
|
|
// Read the current type version from the registry.
|
|
|
|
// This will be used to find any key that contains the JavaHome value.
|
2013-10-05 00:08:13 +00:00
|
|
|
char *value = new char[0];
|
|
|
|
DWORD valueSz = 0;
|
2013-11-04 01:53:05 +00:00
|
|
|
if (RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE *)value, &valueSz) ==
|
|
|
|
ERROR_MORE_DATA)
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
|
|
|
value = new char[valueSz];
|
2013-11-04 01:53:05 +00:00
|
|
|
RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE *)value, &valueSz);
|
2013-10-05 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 18:54:52 +00:00
|
|
|
QString recommended = value;
|
|
|
|
|
|
|
|
TCHAR subKeyName[255];
|
|
|
|
DWORD subKeyNameSize, numSubKeys, retCode;
|
|
|
|
|
|
|
|
// Get the number of subkeys
|
2013-11-04 01:53:05 +00:00
|
|
|
RegQueryInfoKey(jreKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL,
|
|
|
|
NULL, NULL);
|
2013-10-05 00:08:13 +00:00
|
|
|
|
2013-10-06 18:54:52 +00:00
|
|
|
// Iterate until RegEnumKeyEx fails
|
2013-11-04 01:53:05 +00:00
|
|
|
if (numSubKeys > 0)
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
2013-11-04 01:53:05 +00:00
|
|
|
for (int i = 0; i < numSubKeys; i++)
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
2013-10-06 18:54:52 +00:00
|
|
|
subKeyNameSize = 255;
|
2013-11-04 01:53:05 +00:00
|
|
|
retCode = RegEnumKeyEx(jreKey, i, subKeyName, &subKeyNameSize, NULL, NULL, NULL,
|
|
|
|
NULL);
|
|
|
|
if (retCode == ERROR_SUCCESS)
|
2013-10-06 18:54:52 +00:00
|
|
|
{
|
|
|
|
// Now open the registry key for the version that we just got.
|
|
|
|
QString newKeyName = keyName + "\\" + subKeyName;
|
|
|
|
|
|
|
|
HKEY newKey;
|
2013-11-04 01:53:05 +00:00
|
|
|
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, newKeyName.toStdString().c_str(), 0,
|
|
|
|
KEY_READ | KEY_WOW64_64KEY, &newKey) == ERROR_SUCCESS)
|
2013-10-06 18:54:52 +00:00
|
|
|
{
|
|
|
|
// Read the JavaHome value to find where Java is installed.
|
|
|
|
value = new char[0];
|
|
|
|
valueSz = 0;
|
2013-11-04 01:53:05 +00:00
|
|
|
if (RegQueryValueEx(newKey, "JavaHome", NULL, NULL, (BYTE *)value,
|
|
|
|
&valueSz) == ERROR_MORE_DATA)
|
2013-10-06 18:54:52 +00:00
|
|
|
{
|
|
|
|
value = new char[valueSz];
|
2013-11-04 01:53:05 +00:00
|
|
|
RegQueryValueEx(newKey, "JavaHome", NULL, NULL, (BYTE *)value,
|
|
|
|
&valueSz);
|
2013-10-06 18:54:52 +00:00
|
|
|
|
2013-10-14 01:59:21 +00:00
|
|
|
// Now, we construct the version object and add it to the list.
|
|
|
|
JavaVersionPtr javaVersion(new JavaVersion());
|
|
|
|
|
|
|
|
javaVersion->id = subKeyName;
|
|
|
|
javaVersion->arch = archType;
|
2013-11-04 01:53:05 +00:00
|
|
|
javaVersion->path =
|
2014-06-27 22:05:00 +00:00
|
|
|
QDir(PathCombine(value, "bin")).absoluteFilePath("javaw.exe");
|
2013-10-14 01:59:21 +00:00
|
|
|
javas.append(javaVersion);
|
2013-10-06 18:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(newKey);
|
|
|
|
}
|
|
|
|
}
|
2013-10-05 00:08:13 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-06 18:54:52 +00:00
|
|
|
|
|
|
|
RegCloseKey(jreKey);
|
2013-10-05 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 18:54:52 +00:00
|
|
|
return javas;
|
|
|
|
}
|
|
|
|
|
2013-12-12 14:56:32 +00:00
|
|
|
QList<QString> JavaUtils::FindJavaPaths()
|
2013-10-14 01:59:21 +00:00
|
|
|
{
|
2013-12-12 14:56:32 +00:00
|
|
|
QList<JavaVersionPtr> java_candidates;
|
2013-10-14 01:59:21 +00:00
|
|
|
|
2013-11-04 01:53:05 +00:00
|
|
|
QList<JavaVersionPtr> JRE64s = this->FindJavaFromRegistryKey(
|
|
|
|
KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment");
|
|
|
|
QList<JavaVersionPtr> JDK64s = this->FindJavaFromRegistryKey(
|
|
|
|
KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Development Kit");
|
|
|
|
QList<JavaVersionPtr> JRE32s = this->FindJavaFromRegistryKey(
|
|
|
|
KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment");
|
|
|
|
QList<JavaVersionPtr> JDK32s = this->FindJavaFromRegistryKey(
|
|
|
|
KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Development Kit");
|
2013-10-14 01:59:21 +00:00
|
|
|
|
2013-12-12 14:56:32 +00:00
|
|
|
java_candidates.append(JRE64s);
|
2014-06-27 22:05:00 +00:00
|
|
|
java_candidates.append(MakeJavaPtr("C:/Program Files/Java/jre7/bin/javaw.exe"));
|
|
|
|
java_candidates.append(MakeJavaPtr("C:/Program Files/Java/jre6/bin/javaw.exe"));
|
2013-12-12 14:56:32 +00:00
|
|
|
java_candidates.append(JDK64s);
|
|
|
|
java_candidates.append(JRE32s);
|
2014-06-27 22:05:00 +00:00
|
|
|
java_candidates.append(MakeJavaPtr("C:/Program Files (x86)/Java/jre7/bin/javaw.exe"));
|
|
|
|
java_candidates.append(MakeJavaPtr("C:/Program Files (x86)/Java/jre6/bin/javaw.exe"));
|
2013-12-12 14:56:32 +00:00
|
|
|
java_candidates.append(JDK32s);
|
2013-12-12 15:25:02 +00:00
|
|
|
java_candidates.append(MakeJavaPtr(this->GetDefaultJava()->path));
|
2013-12-12 14:56:32 +00:00
|
|
|
|
|
|
|
QList<QString> candidates;
|
|
|
|
for(JavaVersionPtr java_candidate : java_candidates)
|
|
|
|
{
|
|
|
|
if(!candidates.contains(java_candidate->path))
|
|
|
|
{
|
|
|
|
candidates.append(java_candidate->path);
|
|
|
|
}
|
|
|
|
}
|
2013-10-06 18:54:52 +00:00
|
|
|
|
2013-12-11 03:54:39 +00:00
|
|
|
return candidates;
|
2013-10-05 00:08:13 +00:00
|
|
|
}
|
2013-12-11 03:54:39 +00:00
|
|
|
|
2013-10-05 00:08:13 +00:00
|
|
|
#elif OSX
|
2013-12-12 15:04:24 +00:00
|
|
|
QList<QString> JavaUtils::FindJavaPaths()
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
2013-12-12 15:04:24 +00:00
|
|
|
QList<QString> javas;
|
|
|
|
javas.append(this->GetDefaultJava()->path);
|
2014-01-22 17:28:56 +00:00
|
|
|
javas.append("/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/MacOS/itms/java/bin/java");
|
2014-01-02 16:01:26 +00:00
|
|
|
javas.append("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java");
|
|
|
|
javas.append("/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java");
|
2014-01-22 17:28:56 +00:00
|
|
|
QDir libraryJVMDir("/Library/Java/JavaVirtualMachines/");
|
|
|
|
QStringList libraryJVMJavas = libraryJVMDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
foreach (const QString &java, libraryJVMJavas) {
|
|
|
|
javas.append(libraryJVMDir.absolutePath() + "/" + java + "/Contents/Home/bin/java");
|
|
|
|
javas.append(libraryJVMDir.absolutePath() + "/" + java + "/Contents/Home/jre/bin/java");
|
|
|
|
}
|
|
|
|
QDir systemLibraryJVMDir("/System/Library/Java/JavaVirtualMachines/");
|
|
|
|
QStringList systemLibraryJVMJavas = systemLibraryJVMDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
foreach (const QString &java, systemLibraryJVMJavas) {
|
|
|
|
javas.append(systemLibraryJVMDir.absolutePath() + "/" + java + "/Contents/Home/bin/java");
|
|
|
|
javas.append(systemLibraryJVMDir.absolutePath() + "/" + java + "/Contents/Commands/java");
|
|
|
|
}
|
2013-10-14 23:30:33 +00:00
|
|
|
return javas;
|
2013-10-05 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif LINUX
|
2013-12-12 15:04:24 +00:00
|
|
|
QList<QString> JavaUtils::FindJavaPaths()
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
2013-10-06 01:20:19 +00:00
|
|
|
QLOG_INFO() << "Linux Java detection incomplete - defaulting to \"java\"";
|
2013-10-05 00:08:13 +00:00
|
|
|
|
2013-12-12 15:04:24 +00:00
|
|
|
QList<QString> javas;
|
|
|
|
javas.append(this->GetDefaultJava()->path);
|
2014-03-30 19:29:44 +00:00
|
|
|
javas.append("/opt/java/bin/java");
|
|
|
|
javas.append("/usr/bin/java");
|
2013-10-14 23:30:33 +00:00
|
|
|
|
|
|
|
return javas;
|
2013-10-05 00:08:13 +00:00
|
|
|
}
|
|
|
|
#else
|
2013-12-12 15:04:24 +00:00
|
|
|
QList<QString> JavaUtils::FindJavaPaths()
|
2013-10-05 00:08:13 +00:00
|
|
|
{
|
2013-10-06 01:20:19 +00:00
|
|
|
QLOG_INFO() << "Unknown operating system build - defaulting to \"java\"";
|
2013-10-05 00:08:13 +00:00
|
|
|
|
2013-12-12 15:04:24 +00:00
|
|
|
QList<QString> javas;
|
|
|
|
javas.append(this->GetDefaultJava()->path);
|
2013-10-14 23:30:33 +00:00
|
|
|
|
|
|
|
return javas;
|
2013-10-05 00:08:13 +00:00
|
|
|
}
|
|
|
|
#endif
|