|
| 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 | + |
0 commit comments