Skip to content

Commit ada1352

Browse files
committed
Windows launcher updated to prefer 64bit JDK over 32bit and to prefer JDK 8 over JDK 9+
1 parent ead75b3 commit ada1352

File tree

8 files changed

+255
-6
lines changed

8 files changed

+255
-6
lines changed

visualvm/launcher/visualvm.exe

31 KB
Binary file not shown.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
#include "jvmfinder.h"
27+
#include <cassert>
28+
#include <fstream>
29+
30+
using namespace std;
31+
32+
const char *JvmFinder::OLD_JDK_KEY = "Software\\JavaSoft\\Java Development Kit";
33+
const char *JvmFinder::OLD_JRE_KEY = "Software\\JavaSoft\\Java Runtime Environment";
34+
const char *JvmFinder::JDK_KEY = "Software\\JavaSoft\\JDK";
35+
const char *JvmFinder::JRE_KEY = "Software\\JavaSoft\\JRE";
36+
const char *JvmFinder::CUR_VERSION_NAME = "CurrentVersion";
37+
const char *JvmFinder::JAVA_HOME_NAME = "JavaHome";
38+
const char *JvmFinder::JAVA_BIN_DIR = "\\bin";
39+
const char *JvmFinder::JAVA_EXE_FILE = "\\bin\\java.exe";
40+
const char *JvmFinder::JAVAW_EXE_FILE = "\\bin\\javaw.exe";
41+
const char *JvmFinder::JAVA_CLIENT_DLL_FILE = "\\bin\\client\\jvm.dll";
42+
const char *JvmFinder::JAVA_SERVER_DLL_FILE = "\\bin\\server\\jvm.dll";
43+
const char *JvmFinder::JAVA_JRE_PREFIX = "\\jre";
44+
45+
JvmFinder::JvmFinder() {
46+
}
47+
48+
JvmFinder::JvmFinder(const JvmFinder& orig) {
49+
}
50+
51+
JvmFinder::~JvmFinder() {
52+
}
53+
54+
bool JvmFinder::getJavaPath(string &path) {
55+
logMsg("JvmFinder::getJavaPath()");
56+
path = javaPath;
57+
return !javaPath.empty();
58+
}
59+
60+
bool JvmFinder::findJava(const char *minJavaVersion) {
61+
if (find64bitJava(OLD_JDK_KEY, JAVA_JRE_PREFIX, minJavaVersion)) {
62+
return true;
63+
}
64+
if (find64bitJava(JDK_KEY, "", minJavaVersion)) {
65+
return true;
66+
}
67+
if (find32bitJava(OLD_JDK_KEY, JAVA_JRE_PREFIX, minJavaVersion)) {
68+
return true;
69+
}
70+
if (find64bitJava(OLD_JRE_KEY, "", minJavaVersion)) {
71+
return true;
72+
}
73+
if (find64bitJava(JRE_KEY, "", minJavaVersion)) {
74+
return true;
75+
}
76+
if (find32bitJava(OLD_JRE_KEY, "", minJavaVersion)) {
77+
return true;
78+
}
79+
javaPath = "";
80+
return false;
81+
}
82+
83+
bool JvmFinder::find32bitJava(const char *javaKey, const char *prefix, const char *minJavaVersion) {
84+
logMsg("JvmFinder::find32bitJava()\n\tjavaKey: %s\n\tprefix: %s\n\tminJavaVersion: %s", javaKey, prefix, minJavaVersion);
85+
string value;
86+
bool result = false;
87+
if (getStringFromRegistry(HKEY_LOCAL_MACHINE, javaKey, CUR_VERSION_NAME, value)) {
88+
if (value >= minJavaVersion) {
89+
string path;
90+
if (getStringFromRegistry(HKEY_LOCAL_MACHINE, (string(javaKey) + "\\" + value).c_str(), JAVA_HOME_NAME, path)) {
91+
if (*path.rbegin() == '\\') {
92+
path.erase(path.length() - 1, 1);
93+
}
94+
result = checkJava(path.c_str(), prefix);
95+
}
96+
}
97+
}
98+
return result;
99+
}
100+
101+
bool JvmFinder::find64bitJava(const char *javaKey, const char *prefix, const char *minJavaVersion) {
102+
logMsg("JvmFinder::find64bitJava()\n\tjavaKey: %s\n\tprefix: %s\n\tminJavaVersion: %s", javaKey, prefix, minJavaVersion);
103+
string value;
104+
bool result = false;
105+
if(isWow64()) {
106+
if (getStringFromRegistry64bit(HKEY_LOCAL_MACHINE, javaKey, CUR_VERSION_NAME, value)) {
107+
if (value >= minJavaVersion) {
108+
string path;
109+
if (getStringFromRegistry64bit(HKEY_LOCAL_MACHINE, (string(javaKey) + "\\" + value).c_str(), JAVA_HOME_NAME, path)) {
110+
if (*path.rbegin() == '\\') {
111+
path.erase(path.length() - 1, 1);
112+
}
113+
result = checkJava(path.c_str(), prefix);
114+
}
115+
}
116+
}
117+
}
118+
return result;
119+
}
120+
121+
bool JvmFinder::checkJava(const char *path, const char *prefix) {
122+
assert(path);
123+
assert(prefix);
124+
logMsg("checkJava(%s)", path);
125+
javaPath = path;
126+
if (*javaPath.rbegin() == '\\') {
127+
javaPath.erase(javaPath.length() - 1, 1);
128+
}
129+
string javaExePath = javaPath + prefix + JAVA_EXE_FILE;
130+
string javawExePath = javaPath + prefix + JAVAW_EXE_FILE;
131+
string javaClientDllPath = javaPath + prefix + JAVA_CLIENT_DLL_FILE;
132+
string javaServerDllPath = javaPath + prefix + JAVA_SERVER_DLL_FILE;
133+
if (!fileExists(javaClientDllPath.c_str())) {
134+
javaClientDllPath = "";
135+
}
136+
if (!fileExists(javaServerDllPath.c_str())) {
137+
javaServerDllPath = "";
138+
}
139+
string javaBinPath = javaPath + prefix + JAVA_BIN_DIR;
140+
if (fileExists(javaExePath.c_str()) || !javaClientDllPath.empty() || !javaServerDllPath.empty()) {
141+
if (!fileExists(javawExePath.c_str())) {
142+
logMsg("javaw.exe not exists, forcing java.exe");
143+
javawExePath = javaExePath;
144+
}
145+
return true;
146+
}
147+
148+
javaPath.clear();
149+
return false;
150+
}
151+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
#ifndef JVMFINDER_H
27+
#define JVMFINDER_H
28+
29+
#include <windows.h>
30+
#include <string>
31+
#include <list>
32+
#include "o.n.bootstrap/utilsfuncs.h"
33+
34+
class JvmFinder {
35+
static const int MAX_ARGS_LEN = 32*1024;
36+
37+
static const char *JDK_KEY;
38+
static const char *JRE_KEY;
39+
static const char *OLD_JDK_KEY;
40+
static const char *OLD_JRE_KEY;
41+
static const char *CUR_VERSION_NAME;
42+
static const char *JAVA_HOME_NAME;
43+
static const char *JAVA_BIN_DIR;
44+
static const char *JAVA_EXE_FILE;
45+
static const char *JAVAW_EXE_FILE;
46+
static const char *JAVA_CLIENT_DLL_FILE;
47+
static const char *JAVA_SERVER_DLL_FILE;
48+
static const char *JAVA_JRE_PREFIX;
49+
50+
public:
51+
JvmFinder();
52+
virtual ~JvmFinder();
53+
54+
bool findJava(const char *minJavaVersion);
55+
bool getJavaPath(std::string &path);
56+
57+
private:
58+
JvmFinder(const JvmFinder& orig);
59+
60+
bool checkJava(const char *javaPath, const char *prefix);
61+
bool find32bitJava(const char *javaKey, const char *prefix, const char *minJavaVersion);
62+
bool find64bitJava(const char *javaKey, const char *prefix, const char *minJavaVersion);
63+
64+
private:
65+
std::string javaPath;
66+
};
67+
68+
#endif /* JVMFINDER_H */
69+

visualvm/launcher/windows-src/nbproject/Makefile-impl.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ ALLCONFS=visualvm.exe visualvm64.exe
7676
.depcheck-impl:
7777
@echo "# This code depends on make tool being used" >.dep.inc
7878
@if [ -n "${MAKE_VERSION}" ]; then \
79-
echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES}))" >>.dep.inc; \
79+
echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES} \$${TESTOBJECTFILES}))" >>.dep.inc; \
8080
echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \
8181
echo "include \$${DEPFILES}" >>.dep.inc; \
8282
echo "endif" >>.dep.inc; \

visualvm/launcher/windows-src/nbproject/Makefile-visualvm.exe.mk

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
3535

3636
# Object Files
3737
OBJECTFILES= \
38+
${OBJECTDIR}/jvmfinder.o \
3839
${OBJECTDIR}/o.n.bootstrap/utilsfuncs.o \
3940
${OBJECTDIR}/visualvm.o \
4041
${OBJECTDIR}/visualvmlauncher.o
@@ -63,17 +64,22 @@ LDLIBSOPTIONS=visualvm.res
6364
visualvm.exe: ${OBJECTFILES}
6465
${LINK.cc} -o visualvm.exe ${OBJECTFILES} ${LDLIBSOPTIONS} -mwindows -Wl,--nxcompat -Wl,--dynamicbase -Wl,--no-seh
6566

66-
${OBJECTDIR}/o.n.bootstrap/utilsfuncs.o: o.n.bootstrap/utilsfuncs.cpp
67+
${OBJECTDIR}/jvmfinder.o: jvmfinder.cpp
68+
${MKDIR} -p ${OBJECTDIR}
69+
${RM} "$@.d"
70+
$(COMPILE.cc) -O2 -s -DARCHITECTURE=32 -DNBEXEC_DLL=\"/lib/nbexec.dll\" -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/jvmfinder.o jvmfinder.cpp
71+
72+
${OBJECTDIR}/o.n.bootstrap/utilsfuncs.o: o.n.bootstrap/utilsfuncs.cpp
6773
${MKDIR} -p ${OBJECTDIR}/o.n.bootstrap
6874
${RM} "$@.d"
6975
$(COMPILE.cc) -O2 -s -DARCHITECTURE=32 -DNBEXEC_DLL=\"/lib/nbexec.dll\" -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/o.n.bootstrap/utilsfuncs.o o.n.bootstrap/utilsfuncs.cpp
7076

71-
${OBJECTDIR}/visualvm.o: visualvm.cpp
77+
${OBJECTDIR}/visualvm.o: visualvm.cpp
7278
${MKDIR} -p ${OBJECTDIR}
7379
${RM} "$@.d"
7480
$(COMPILE.cc) -O2 -s -DARCHITECTURE=32 -DNBEXEC_DLL=\"/lib/nbexec.dll\" -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/visualvm.o visualvm.cpp
7581

76-
${OBJECTDIR}/visualvmlauncher.o: visualvmlauncher.cpp
82+
${OBJECTDIR}/visualvmlauncher.o: visualvmlauncher.cpp
7783
${MKDIR} -p ${OBJECTDIR}
7884
${RM} "$@.d"
7985
$(COMPILE.cc) -O2 -s -DARCHITECTURE=32 -DNBEXEC_DLL=\"/lib/nbexec.dll\" -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/visualvmlauncher.o visualvmlauncher.cpp
@@ -84,7 +90,6 @@ ${OBJECTDIR}/visualvmlauncher.o: visualvmlauncher.cpp
8490
# Clean Targets
8591
.clean-conf: ${CLEAN_SUBPROJECTS}
8692
${RM} -r ${CND_BUILDDIR}/${CND_CONF}
87-
${RM} visualvm.exe
8893

8994
# Subprojects
9095
.clean-subprojects:

visualvm/launcher/windows-src/nbproject/configurations.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<configurationDescriptor version="90">
2+
<configurationDescriptor version="100">
33
<logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
44
<logicalFolder name="HeaderFiles"
55
displayName="Header Files"
66
projectFiles="true">
77
<itemPath>cmdargs.h</itemPath>
8+
<itemPath>jvmfinder.h</itemPath>
89
<itemPath>o.n.bootstrap/utilsfuncs.h</itemPath>
910
<itemPath>version.h</itemPath>
1011
<itemPath>visualvmlauncher.h</itemPath>
@@ -19,6 +20,7 @@
1920
<logicalFolder name="SourceFiles"
2021
displayName="Source Files"
2122
projectFiles="true">
23+
<itemPath>jvmfinder.cpp</itemPath>
2224
<itemPath>o.n.bootstrap/utilsfuncs.cpp</itemPath>
2325
<itemPath>visualvm.cpp</itemPath>
2426
<itemPath>visualvmlauncher.cpp</itemPath>
@@ -58,6 +60,10 @@
5860
</compileType>
5961
<item path="cmdargs.h" ex="false" tool="3" flavor2="0">
6062
</item>
63+
<item path="jvmfinder.cpp" ex="false" tool="1" flavor2="0">
64+
</item>
65+
<item path="jvmfinder.h" ex="false" tool="3" flavor2="0">
66+
</item>
6167
<item path="o.n.bootstrap/utilsfuncs.cpp" ex="false" tool="1" flavor2="0">
6268
</item>
6369
<item path="o.n.bootstrap/utilsfuncs.h" ex="false" tool="3" flavor2="0">
@@ -104,6 +110,10 @@
104110
</compileType>
105111
<item path="cmdargs.h" ex="false" tool="3" flavor2="0">
106112
</item>
113+
<item path="jvmfinder.cpp" ex="false" tool="1" flavor2="0">
114+
</item>
115+
<item path="jvmfinder.h" ex="false" tool="3" flavor2="0">
116+
</item>
107117
<item path="o.n.bootstrap/utilsfuncs.cpp" ex="false" tool="1" flavor2="0">
108118
</item>
109119
<item path="o.n.bootstrap/utilsfuncs.h" ex="false" tool="3" flavor2="0">

visualvm/launcher/windows-src/visualvmlauncher.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
using namespace std;
3838

39+
const char *VisualVMLauncher::REQ_JAVA_VERSION = "1.8";
3940
const char *VisualVMLauncher::NBEXEC_FILE_PATH = NBEXEC_DLL;
4041
const char *VisualVMLauncher::OPT_VISUALVM_DEFAULT_USER_DIR = "visualvm_default_userdir=";
4142
const char *VisualVMLauncher::OPT_VISUALVM_DEFAULT_CACHE_DIR = "visualvm_default_cachedir=";
@@ -131,6 +132,11 @@ int VisualVMLauncher::start(int argc, char *argv[]) {
131132
for (int i = 0; i < argc; i++) {
132133
newArgs.add(argv[i]);
133134
}
135+
if (!jdkOptionFound && jdkHome.empty()) {
136+
if (jvmFinder.findJava(REQ_JAVA_VERSION)) {
137+
jvmFinder.getJavaPath(jdkHome);
138+
}
139+
}
134140
if (!jdkHome.empty()) {
135141
newArgs.add(ARG_NAME_JDKHOME);
136142
newArgs.add(jdkHome.c_str());
@@ -321,6 +327,7 @@ bool VisualVMLauncher::parseArgs(int argc, char *argv[]) {
321327
logMsg("\t%s", argv[i]);
322328
}
323329
customUserDirFound = 0;
330+
jdkOptionFound = false;
324331
for (int i = 0; i < argc; i++) {
325332
if (strcmp(ARG_NAME_USER_DIR, argv[i]) == 0) {
326333
CHECK_ARG;
@@ -345,6 +352,9 @@ bool VisualVMLauncher::parseArgs(int argc, char *argv[]) {
345352
cacheDir = tmp;
346353
logMsg("Cache dir: %s", cacheDir.c_str());
347354
}
355+
if (strcmp(ARG_NAME_JDKHOME, argv[i]) == 0) {
356+
jdkOptionFound = true;
357+
}
348358
}
349359
logMsg("parseArgs() finished");
350360
return true;

visualvm/launcher/windows-src/visualvmlauncher.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
#include <windows.h>
3131
#include <cstddef>
3232
#include "cmdargs.h"
33+
#include "jvmfinder.h"
3334

3435
class VisualVMLauncher {
3536
protected:
37+
static const char *REQ_JAVA_VERSION;
3638
static const char *NBEXEC_FILE_PATH;
3739
static const char *OPT_VISUALVM_DEFAULT_USER_DIR;
3840
static const char *OPT_VISUALVM_DEFAULT_CACHE_DIR;
@@ -102,9 +104,11 @@ class VisualVMLauncher {
102104
std::string extraClusters;
103105
std::string nbOptions;
104106
std::string jdkHome;
107+
bool jdkOptionFound;
105108

106109
private:
107110
bool customUserDirFound;
111+
JvmFinder jvmFinder;
108112
};
109113

110114
#endif /* _NBLAUNCHER_H */

0 commit comments

Comments
 (0)