Skip to content

Commit 4647794

Browse files
committed
Added support for java 32 and 64 bit versions
1 parent 6a2c35d commit 4647794

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/api/JavaIntegration.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <QFileInfo>
1010
#include <QTextStream>
1111
#include <QDebug>
12+
#include <QSettings>
13+
#include <QApplication>
1214
#include <string>
1315
#include <algorithm>
1416
#include <random>
@@ -45,7 +47,7 @@ void JavaIntegration::startAPIServerCmd() {
4547
+ jarPath + "\" com.mygcc.api.Main";
4648
javaProcess.start(fullStr.c_str());
4749
} else {
48-
qDebug("%s", "Invalid java version");
50+
qDebug() << "Invalid java version";
4951

5052
// Open site to show user how to install java
5153
QDesktopServices::openUrl(QUrl(INSTALL_JAVA_SITE));
@@ -77,9 +79,61 @@ std::string* JavaIntegration::findJava() {
7779
return new std::string(JAVA_PATH_MAC);
7880
} else {
7981
qDebug() << "Known macOS java path NOT found";
82+
return new std::string("");
8083
}
8184
#endif
8285

86+
// User registry keys for finding java on Windows
87+
#if defined(_WIN32) || defined(_WIN64)
88+
// 64 bit registry key
89+
QSettings jreKey64("HKEY_LOCAL_MACHINE\\SOFTWARE" \
90+
"\\JavaSoft\\Java Runtime Environment",
91+
QSettings::Registry64Format);
92+
93+
// 32 bit registry key
94+
QSettings jreKey32("HKEY_LOCAL_MACHINE\\Software\\WOW6432Node" \
95+
"\\JavaSoft\\Java Runtime Environment",
96+
QSettings::Registry32Format);
97+
98+
// If JAVA installed
99+
if (jreKey32.allKeys().contains("CurrentVersion", Qt::CaseInsensitive) ||
100+
jreKey64.allKeys().contains("CurrentVersion", Qt::CaseInsensitive)) {
101+
QSettings *jreKey;
102+
103+
if (jreKey32.allKeys().contains("CurrentVersion", Qt::CaseInsensitive)) {
104+
qDebug("32 bit Java FOUND");
105+
jreKey = &jreKey32;
106+
}
107+
if (jreKey64.allKeys().contains("CurrentVersion", Qt::CaseInsensitive)) {
108+
qDebug("64 bit JAVA FOUND");
109+
jreKey = &jreKey64;
110+
}
111+
112+
auto version = jreKey->value("CurrentVersion").toString();
113+
qDebug() << "Latest java version identified:" <<
114+
version.toStdString().c_str();
115+
if (version.contains("1.8")) {
116+
QSettings jreVerKey(jreKey->fileName() + "\\" + version,
117+
QSettings::Registry64Format);
118+
119+
if (jreVerKey.childKeys().contains("JavaHome", Qt::CaseInsensitive)) {
120+
auto javaHome = jreVerKey.value("JavaHome").toString();
121+
qDebug() << "Java home identified:" << javaHome.toStdString().c_str();
122+
123+
return new std::string(javaHome.toStdString() + "\\bin\\java.exe");
124+
} else {
125+
qWarning("JavaHome key NOT FOUND");
126+
}
127+
} else {
128+
qWarning("Java version '1.8' NOT FOUND");
129+
}
130+
} else {
131+
qWarning("Java NOT FOUND in default install path");
132+
}
133+
return new std::string("");
134+
#endif
135+
136+
#if defined(__unix) || defined(__unix__) || defined(__linux__)
83137
// Check for java in $PATH
84138
QProcess findJavaExe;
85139
findJavaExe.start("java");
@@ -96,6 +150,7 @@ std::string* JavaIntegration::findJava() {
96150
} else {
97151
return new std::string("");
98152
}
153+
#endif
99154
}
100155

101156
bool JavaIntegration::checkJavaVersion(std::string *javaPath) {

0 commit comments

Comments
 (0)