summaryrefslogtreecommitdiffstats
path: root/logic/JavaUtils.cpp
blob: d89ab9c5d2f535b440f2d900ac200655a8d0a923 (plain)
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
/* 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 "JavaUtils.h"
#include "osutils.h"
#include "pathutils.h"

#include <QStringList>
#include <QString>
#include <QDir>
#include <QDebug>

#if WINDOWS
#include <windows.h>

#endif

JavaUtils::JavaUtils()
{

}

#if WINDOWS
QStringList JavaUtils::FindJavaPath()
{
	QStringList paths;

	HKEY jreKey;
	QString jreKeyName = "SOFTWARE\\JavaSoft\\Java Runtime Environment";
	if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, jreKeyName.toStdString().c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &jreKey) == ERROR_SUCCESS)
	{
		// Read the current JRE version from the registry.
		// This will be used to find the key that contains the JavaHome value.
		char *value = new char[0];
		DWORD valueSz = 0;
		if (RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE*)value, &valueSz) == ERROR_MORE_DATA)
		{
			value = new char[valueSz];
			RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE*)value, &valueSz);
		}

		RegCloseKey(jreKey);

		// Now open the registry key for the JRE version that we just got.
		jreKeyName.append("\\").append(value);
		if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, jreKeyName.toStdString().c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &jreKey) == ERROR_SUCCESS)
		{
			// Read the JavaHome value to find where Java is installed.
			value = new char[0];
			valueSz = 0;
			if (RegQueryValueExA(jreKey, "JavaHome", NULL, NULL, (BYTE*)value, &valueSz) == ERROR_MORE_DATA)
			{
				value = new char[valueSz];
				RegQueryValueExA(jreKey, "JavaHome", NULL, NULL, (BYTE*)value, &valueSz);

				paths << QDir(PathCombine(value, "bin")).absoluteFilePath("java.exe");
			}

			RegCloseKey(jreKey);
		}
	}

	if(paths.length() <= 0)
	{
		qWarning() << "Failed to find Java in the Windows registry - defaulting to \"java\"";
		paths << "java";
	}

	return paths;
}
#elif OSX
QStringList JavaUtils::FindJavaPath()
{
	qWarning() << "OS X Java detection incomplete - defaulting to \"java\"";

	QStringList paths;
	paths << "java";

	return paths;
}

#elif LINUX
QStringList JavaUtils::FindJavaPath()
{
	qWarning() << "Linux Java detection incomplete - defaulting to \"java\"";

	QStringList paths;
	paths << "java";

	return paths;
}
#else
QStringList JavaUtils::FindJavaPath()
{
	qWarning() << "Unknown operating system build - defaulting to \"java\"";

	QStringList paths;
	paths << "java";

	return paths;
}
#endif