aboutsummaryrefslogtreecommitdiffstats
path: root/find_java
diff options
context:
space:
mode:
authorDavid Herman <davidherman@google.com>2014-10-17 14:58:10 -0700
committerDavid Herman <davidherman@google.com>2014-10-23 11:29:54 -0700
commit1b9e6f635463f50548ef24e83ba73c8efd6b6fb3 (patch)
treeb7bdd5c5802b4d9b6551c5491f0a21b785908f8a /find_java
parent127af49b6548e0a20cb02fa8ce86c90d1da7f875 (diff)
downloadsdk-1b9e6f635463f50548ef24e83ba73c8efd6b6fb3.zip
sdk-1b9e6f635463f50548ef24e83ba73c8efd6b6fb3.tar.gz
sdk-1b9e6f635463f50548ef24e83ba73c8efd6b6fb3.tar.bz2
New -jdk option for find_java
find_java traditionally searched for any java.exe it could find, but recent requirements ask us to find versions of java.exe that are bundled in a JDK. This can now be done using the -jdk flag. Change-Id: I3aaba9c74271f0529d7fa16ff822b999059d578c
Diffstat (limited to 'find_java')
-rwxr-xr-xfind_java/src/source/find_java.h9
-rw-r--r--find_java/src/source/find_java_exe.cpp36
-rwxr-xr-xfind_java/src/source/find_java_lib.cpp81
3 files changed, 84 insertions, 42 deletions
diff --git a/find_java/src/source/find_java.h b/find_java/src/source/find_java.h
index 3b60111..8115729 100755
--- a/find_java/src/source/find_java.h
+++ b/find_java/src/source/find_java.h
@@ -26,10 +26,11 @@
#define MIN_JAVA_VERSION_MINOR 6
#define MIN_JAVA_VERSION (MIN_JAVA_VERSION_MAJOR * 1000 + MIN_JAVA_VERSION_MINOR)
-int checkJavaInPath(const CPath &path);
-int findJavaInEnvPath(CPath *outJavaPath);
-int findJavaInRegistry(CPath *outJavaPath);
-int findJavaInProgramFiles(CPath *outJavaPath);
+int checkJavaInPath(const CPath &path, bool isJdk = false);
+int findJavaInEnvPath(CPath *outJavaPath, bool isJdk = false);
+int findJavaInRegistry(CPath *outJavaPath, bool isJdk = false);
+int findJavaInProgramFiles(CPath *outJavaPath, bool isJdk = false);
+
bool getJavaVersion(CPath &javaPath, CString *outVersionStr, int *outVersionInt);
#endif /* _WIN32 */
diff --git a/find_java/src/source/find_java_exe.cpp b/find_java/src/source/find_java_exe.cpp
index fa5746a..12cf095 100644
--- a/find_java/src/source/find_java_exe.cpp
+++ b/find_java/src/source/find_java_exe.cpp
@@ -43,21 +43,29 @@
#include <io.h>
#include <fcntl.h>
-static void testFindJava() {
+static void testFindJava(bool isJdk) {
CPath javaPath("<not found>");
- int v = findJavaInEnvPath(&javaPath);
- printf("findJavaInEnvPath: [%d] %s\n", v, javaPath.cstr());
+ int v = findJavaInEnvPath(&javaPath, isJdk);
+ printf(" findJavaInEnvPath: [%d] %s\n", v, javaPath.cstr());
javaPath.set("<not found>");
- v = findJavaInRegistry(&javaPath);
- printf("findJavaInRegistry [%d] %s\n", v, javaPath.cstr());
+ v = findJavaInRegistry(&javaPath, isJdk);
+ printf(" findJavaInRegistry [%d] %s\n", v, javaPath.cstr());
javaPath.set("<not found>");
- v = findJavaInProgramFiles(&javaPath);
- printf("findJavaInProgramFiles [%d] %s\n", v, javaPath.cstr());
+ v = findJavaInProgramFiles(&javaPath, isJdk);
+ printf(" findJavaInProgramFiles [%d] %s\n", v, javaPath.cstr());
}
+static void testFindJava() {
+ printf("Searching for any java.exe:\n");
+ testFindJava(false);
+
+ printf("\n");
+ printf("Searching for java.exe within a JDK:\n");
+ testFindJava(true);
+}
int main(int argc, char* argv[]) {
@@ -66,12 +74,16 @@ int main(int argc, char* argv[]) {
bool doShortPath = false;
bool doVersion = false;
bool doJavaW = false;
+ bool isJdk = false;
for (int i = 1; i < argc; i++) {
if (strncmp(argv[i], "-t", 2) == 0) {
testFindJava();
return 0;
+ } else if (strncmp(argv[i], "-j", 2) == 0) {
+ isJdk = true;
+
} else if (strncmp(argv[i], "-d", 2) == 0) {
gIsDebug = true;
@@ -84,13 +96,15 @@ int main(int argc, char* argv[]) {
} else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "-javaw") == 0) {
doJavaW = true;
- } else {
+ }
+ else {
printf(
"Outputs the path of the first Java.exe found on the local system.\n"
"Returns code 0 when found, 1 when not found.\n"
"Options:\n"
"-h / -help : This help.\n"
"-t / -test : Internal test.\n"
+ "-j / -jdk : Only returns java.exe found in a JDK.\n"
"-s / -short : Print path in short DOS form.\n"
"-w / -javaw : Search a matching javaw.exe; defaults to java.exe if not found.\n"
"-v / -version: Only prints the Java version found.\n"
@@ -101,12 +115,12 @@ int main(int argc, char* argv[]) {
// Find the first suitable version of Java we can use.
CPath javaPath;
- int version = findJavaInEnvPath(&javaPath);
+ int version = findJavaInEnvPath(&javaPath, isJdk);
if (version < MIN_JAVA_VERSION) {
- version = findJavaInRegistry(&javaPath);
+ version = findJavaInRegistry(&javaPath, isJdk);
}
if (version < MIN_JAVA_VERSION) {
- version = findJavaInProgramFiles(&javaPath);
+ version = findJavaInProgramFiles(&javaPath, isJdk);
}
if (version < MIN_JAVA_VERSION || javaPath.isEmpty()) {
if (gIsDebug) {
diff --git a/find_java/src/source/find_java_lib.cpp b/find_java/src/source/find_java_lib.cpp
index 95d0729..bf69546 100755
--- a/find_java/src/source/find_java_lib.cpp
+++ b/find_java/src/source/find_java_lib.cpp
@@ -81,6 +81,21 @@ static bool extractJavaVersion(const char *start,
return false;
}
+// Check if the passed in path is a path to a JDK
+static bool isJdkPath(const CPath &path) {
+
+ // If the files "bin/java.exe" and "lib/tools.jar" exist, we're probably a JDK.
+ CPath pathBin(path);
+ pathBin.addPath("bin");
+ pathBin.addPath("java.exe");
+
+ CPath pathTools(path);
+ pathTools.addPath("lib");
+ pathTools.addPath("tools.jar");
+
+ return (pathBin.fileExists() && pathTools.fileExists());
+}
+
// Check whether we can find $PATH/java.exe.
// inOutPath should be the directory where we're looking at.
// In output, it will be the java path we tested.
@@ -125,7 +140,7 @@ int checkJavaInPath(const CPath &path) {
}
// Search java.exe in the environment
-int findJavaInEnvPath(CPath *outJavaPath) {
+int findJavaInEnvPath(CPath *outJavaPath, bool isJdk) {
SetLastError(0);
int currVersion = 0;
@@ -133,17 +148,20 @@ int findJavaInEnvPath(CPath *outJavaPath) {
const char* envPath = getenv("JAVA_HOME");
if (envPath != NULL) {
CPath p(envPath);
- currVersion = checkBinPath(&p);
- if (currVersion > 0) {
- if (gIsDebug) {
- fprintf(stderr, "Java %d found via JAVA_HOME: %s\n", currVersion, p.cstr());
+
+ if (!isJdk || isJdkPath(p)) {
+ currVersion = checkBinPath(&p);
+ if (currVersion > 0) {
+ if (gIsDebug) {
+ fprintf(stderr, "Java %d found via JAVA_HOME: %s\n", currVersion, p.cstr());
+ }
+ *outJavaPath = p;
+ }
+ if (currVersion >= MIN_JAVA_VERSION) {
+ // As an optimization for runtime, if we find a suitable java
+ // version in JAVA_HOME we won't waste time looking at the PATH.
+ return currVersion;
}
- *outJavaPath = p;
- }
- if (currVersion >= MIN_JAVA_VERSION) {
- // As an optimization for runtime, if we find a suitable java
- // version in JAVA_HOME we won't waste time looking at the PATH.
- return currVersion;
}
}
@@ -156,11 +174,17 @@ int findJavaInEnvPath(CPath *outJavaPath) {
CArray<CString> *paths = CString(envPath).split(';');
for(int i = 0; i < paths->size(); i++) {
CPath p((*paths)[i].cstr());
+
+ if (isJdk && !isJdkPath(p)) {
+ continue;
+ }
+
int v = checkPath(&p);
if (v > currVersion) {
if (gIsDebug) {
fprintf(stderr, "Java %d found via env PATH: %s\n", v, p.cstr());
}
+
currVersion = v;
*outJavaPath = p;
}
@@ -313,19 +337,18 @@ static bool getMaxJavaInRegistry(const char *entry, REGSAM access, CPath *outJav
return false;
}
-int findJavaInRegistry(CPath *outJavaPath) {
+int findJavaInRegistry(CPath *outJavaPath, bool isJdk) {
// Check the JRE first, then the JDK.
int version = MIN_JAVA_VERSION - 1;
bool result = false;
- result |= getMaxJavaInRegistry("Java Runtime Environment", 0, outJavaPath, &version);
- result |= getMaxJavaInRegistry("Java Development Kit", 0, outJavaPath, &version);
+ result |= (!isJdk && getMaxJavaInRegistry("Java Runtime Environment", 0, outJavaPath, &version));
+ result |= getMaxJavaInRegistry("Java Development Kit", 0, outJavaPath, &version);
// Even if we're 64-bit, try again but check the 32-bit registry, looking for 32-bit java.
if (isApplication64()) {
- result |= getMaxJavaInRegistry(
- "Java Runtime Environment", KEY_WOW64_32KEY, outJavaPath, &version);
- result |= getMaxJavaInRegistry(
- "Java Development Kit", KEY_WOW64_32KEY, outJavaPath, &version);
+ result |= (!isJdk &&
+ getMaxJavaInRegistry("Java Runtime Environment", KEY_WOW64_32KEY, outJavaPath, &version));
+ result |= getMaxJavaInRegistry("Java Development Kit", KEY_WOW64_32KEY, outJavaPath, &version);
}
return result ? version : 0;
@@ -333,7 +356,8 @@ int findJavaInRegistry(CPath *outJavaPath) {
// --------------
-static bool checkProgramFiles(CPath *outJavaPath, int *inOutVersion, bool force32bit) {
+static bool checkProgramFiles(CPath *outJavaPath, int *inOutVersion, bool isJdk,
+ bool force32bit) {
char programFilesPath[MAX_PATH + 1];
int nFolder = force32bit ? CSIDL_PROGRAM_FILESX86 : CSIDL_PROGRAM_FILES;
@@ -363,11 +387,14 @@ static bool checkProgramFiles(CPath *outJavaPath, int *inOutVersion, bool force3
CPath temp(path);
temp.addPath(findData.cFileName);
// Check C:\Program Files\Java\j*\bin\java.exe
- int v = checkBinPath(&temp);
- if (v > *inOutVersion) {
- found = true;
- *inOutVersion = v;
- *outJavaPath = temp;
+
+ if (!isJdk || isJdkPath(temp)) {
+ int v = checkBinPath(&temp);
+ if (v > *inOutVersion) {
+ found = true;
+ *inOutVersion = v;
+ *outJavaPath = temp;
+ }
}
}
} while (!found && FindNextFileA(findH, &findData) != 0);
@@ -376,17 +403,17 @@ static bool checkProgramFiles(CPath *outJavaPath, int *inOutVersion, bool force3
return found;
}
-int findJavaInProgramFiles(CPath *outJavaPath) {
+int findJavaInProgramFiles(CPath *outJavaPath, bool isJdk) {
// Check the C:\Program Files directory
bool result = false;
int version = MIN_JAVA_VERSION - 1;
- result |= checkProgramFiles(outJavaPath, &version, false);
+ result |= checkProgramFiles(outJavaPath, &version, isJdk, false);
// Even if we're 64-bit, try again but check the C:\Program Files (x86) directory, looking for
// 32-bit java.
if (isApplication64()) {
- result |= checkProgramFiles(outJavaPath, &version, true);
+ result |= checkProgramFiles(outJavaPath, &version, isJdk, true);
}
return result ? version : 0;