Skip to content

Commit

Permalink
GH-112 use JAVA_HOME and JDK_HOME env. variable to find JDK
Browse files Browse the repository at this point in the history
  • Loading branch information
thurka committed May 9, 2024
1 parent 1205a47 commit 9ddfbfa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
Binary file modified visualvm/launcher/visualvm.exe
Binary file not shown.
31 changes: 23 additions & 8 deletions visualvm/launcher/windows-src/jvmfinder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -41,6 +41,8 @@ const char *JvmFinder::JAVAW_EXE_FILE = "\\bin\\javaw.exe";
const char *JvmFinder::JAVA_CLIENT_DLL_FILE = "\\bin\\client\\jvm.dll";
const char *JvmFinder::JAVA_SERVER_DLL_FILE = "\\bin\\server\\jvm.dll";
const char *JvmFinder::JAVA_JRE_PREFIX = "\\jre";
const char *JvmFinder::ENV_JDK_HOME = "JDK_HOME";
const char *JvmFinder::ENV_JAVA_HOME = "JAVA_HOME";

JvmFinder::JvmFinder() {
}
Expand All @@ -67,6 +69,12 @@ bool JvmFinder::findJava(const char *minJavaVersion) {
if (find32bitJava(OLD_JDK_KEY, JAVA_JRE_PREFIX, minJavaVersion)) {
return true;
}
if (findEnvJava(ENV_JDK_HOME)) {
return true;
}
if (findEnvJava(ENV_JAVA_HOME)) {
return true;
}
if (find64bitJava(OLD_JRE_KEY, "", minJavaVersion)) {
return true;
}
Expand All @@ -88,9 +96,6 @@ bool JvmFinder::find32bitJava(const char *javaKey, const char *prefix, const cha
if (value >= minJavaVersion) {
string path;
if (getStringFromRegistry(HKEY_LOCAL_MACHINE, (string(javaKey) + "\\" + value).c_str(), JAVA_HOME_NAME, path)) {
if (*path.rbegin() == '\\') {
path.erase(path.length() - 1, 1);
}
result = checkJava(path.c_str(), prefix);
}
}
Expand All @@ -107,9 +112,6 @@ bool JvmFinder::find64bitJava(const char *javaKey, const char *prefix, const cha
if (value >= minJavaVersion) {
string path;
if (getStringFromRegistry64bit(HKEY_LOCAL_MACHINE, (string(javaKey) + "\\" + value).c_str(), JAVA_HOME_NAME, path)) {
if (*path.rbegin() == '\\') {
path.erase(path.length() - 1, 1);
}
result = checkJava(path.c_str(), prefix);
}
}
Expand All @@ -118,6 +120,19 @@ bool JvmFinder::find64bitJava(const char *javaKey, const char *prefix, const cha
return result;
}

bool JvmFinder::findEnvJava(const char *envVar) {
logMsg("JvmFinder::findEnvJava()\n\tenvVar: %s", envVar);
bool result = false;
char *envJavaPath = getenv(envVar);
if (envJavaPath) {
if (checkJava(envJavaPath, "")) {
return true;
}
result = checkJava(envJavaPath, JAVA_JRE_PREFIX);
}
return result;
}

bool JvmFinder::checkJava(const char *path, const char *prefix) {
assert(path);
assert(prefix);
Expand All @@ -139,7 +154,7 @@ bool JvmFinder::checkJava(const char *path, const char *prefix) {
string javaBinPath = javaPath + prefix + JAVA_BIN_DIR;
if (fileExists(javaExePath.c_str()) || !javaClientDllPath.empty() || !javaServerDllPath.empty()) {
if (!fileExists(javawExePath.c_str())) {
logMsg("javaw.exe not exists, forcing java.exe");
logMsg("javaw.exe does not exists, forcing java.exe");
javawExePath = javaExePath;
}
return true;
Expand Down
5 changes: 4 additions & 1 deletion visualvm/launcher/windows-src/jvmfinder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -46,6 +46,8 @@ class JvmFinder {
static const char *JAVA_CLIENT_DLL_FILE;
static const char *JAVA_SERVER_DLL_FILE;
static const char *JAVA_JRE_PREFIX;
static const char *ENV_JDK_HOME;
static const char *ENV_JAVA_HOME;

public:
JvmFinder();
Expand All @@ -60,6 +62,7 @@ class JvmFinder {
bool checkJava(const char *javaPath, const char *prefix);
bool find32bitJava(const char *javaKey, const char *prefix, const char *minJavaVersion);
bool find64bitJava(const char *javaKey, const char *prefix, const char *minJavaVersion);
bool JvmFinder::findEnvJava(const char *envVar);

private:
std::string javaPath;
Expand Down
4 changes: 2 additions & 2 deletions visualvm/launcher/windows-src/version.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,7 +29,7 @@
#define FVER 0,0,0,0
#define BUILD_ID "0"
#define INTERNAL_NAME "visualvm"
#define COPYRIGHT "\xA9 2007, 2014, Oracle and/or its affiliates. All rights reserved."
#define COPYRIGHT "\xA9 2007, 2024, Oracle and/or its affiliates. All rights reserved."
#define FNAME "visualvm.exe"
#define NAME "VisualVM Dev. build"

0 comments on commit 9ddfbfa

Please sign in to comment.