diff options
Diffstat (limited to 'find_java/src')
-rwxr-xr-x | find_java/src/source/find_java.h | 19 | ||||
-rw-r--r-- | find_java/src/source/find_java_exe.cpp | 87 | ||||
-rwxr-xr-x | find_java/src/source/find_java_lib.cpp | 26 |
3 files changed, 85 insertions, 47 deletions
diff --git a/find_java/src/source/find_java.h b/find_java/src/source/find_java.h index 8115729..fe7da4d 100755 --- a/find_java/src/source/find_java.h +++ b/find_java/src/source/find_java.h @@ -21,15 +21,22 @@ #include "utils.h"
-// We currently search for a Java version for at least 1.6
+#define TO_JAVA_VERSION(major, minor) ((major) * 1000 + (minor))
+#define JAVA_MAJOR(version) ((version) / 1000)
+#define JAVA_MINOR(version) ((version) % 1000)
+
+// We currently search for a Java version for at least 1.6 by default
#define MIN_JAVA_VERSION_MAJOR 1
#define MIN_JAVA_VERSION_MINOR 6
-#define MIN_JAVA_VERSION (MIN_JAVA_VERSION_MAJOR * 1000 + MIN_JAVA_VERSION_MINOR)
+#define MIN_JAVA_VERSION TO_JAVA_VERSION(MIN_JAVA_VERSION_MAJOR, MIN_JAVA_VERSION_MINOR)
-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);
+int checkJavaInPath(const CPath &path, bool isJdk = false, int minVersion = MIN_JAVA_VERSION);
+int findJavaInEnvPath(CPath *outJavaPath, bool isJdk = false,
+ int minVersion = MIN_JAVA_VERSION);
+int findJavaInRegistry(CPath *outJavaPath, bool isJdk = false,
+ int minVersion = MIN_JAVA_VERSION);
+int findJavaInProgramFiles(CPath *outJavaPath, bool isJdk = false,
+ int minVersion = MIN_JAVA_VERSION);
bool getJavaVersion(CPath &javaPath, CString *outVersionStr, int *outVersionInt);
diff --git a/find_java/src/source/find_java_exe.cpp b/find_java/src/source/find_java_exe.cpp index 12cf095..f14c037 100644 --- a/find_java/src/source/find_java_exe.cpp +++ b/find_java/src/source/find_java_exe.cpp @@ -43,28 +43,61 @@ #include <io.h>
#include <fcntl.h>
-static void testFindJava(bool isJdk) {
+static int showHelpMessage() {
+ 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"
+ "-m / -minv # : Pass in a minimum version to use (default: 1.6).\n"
+ "-v / -version: Only prints the Java version found.\n"
+ );
+ return 2;
+}
+
+static void testFindJava(bool isJdk, int minVersion) {
CPath javaPath("<not found>");
- int v = findJavaInEnvPath(&javaPath, isJdk);
+ int v = findJavaInEnvPath(&javaPath, isJdk, minVersion);
printf(" findJavaInEnvPath: [%d] %s\n", v, javaPath.cstr());
javaPath.set("<not found>");
- v = findJavaInRegistry(&javaPath, isJdk);
+ v = findJavaInRegistry(&javaPath, isJdk, minVersion);
printf(" findJavaInRegistry [%d] %s\n", v, javaPath.cstr());
javaPath.set("<not found>");
- v = findJavaInProgramFiles(&javaPath, isJdk);
+ v = findJavaInProgramFiles(&javaPath, isJdk, minVersion);
printf(" findJavaInProgramFiles [%d] %s\n", v, javaPath.cstr());
}
-static void testFindJava() {
+static void testFindJava(int minVersion) {
+
+ printf("Searching for version %d.%d or newer...\n", JAVA_MAJOR(minVersion),
+ JAVA_MINOR(minVersion));
+
+ printf("\n");
printf("Searching for any java.exe:\n");
- testFindJava(false);
+ testFindJava(false, minVersion);
printf("\n");
printf("Searching for java.exe within a JDK:\n");
- testFindJava(true);
+ testFindJava(true, minVersion);
+}
+
+// Returns 0 on failure or a java version on success.
+int parseMinVersionArg(char* arg) {
+
+ int versionMajor = -1;
+ int versionMinor = -1;
+ if (sscanf(arg, "%d.%d", &versionMajor, &versionMinor) != 2) {
+ // -m arg is malformatted
+ return 0;
+ }
+ return TO_JAVA_VERSION(versionMajor, versionMinor);
}
int main(int argc, char* argv[]) {
@@ -75,10 +108,11 @@ int main(int argc, char* argv[]) { bool doVersion = false;
bool doJavaW = false;
bool isJdk = false;
+ int minVersion = MIN_JAVA_VERSION;
for (int i = 1; i < argc; i++) {
if (strncmp(argv[i], "-t", 2) == 0) {
- testFindJava();
+ testFindJava(minVersion);
return 0;
} else if (strncmp(argv[i], "-j", 2) == 0) {
@@ -93,36 +127,33 @@ int main(int argc, char* argv[]) { } else if (strncmp(argv[i], "-v", 2) == 0) {
doVersion = true;
- } else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "-javaw") == 0) {
+ } else if (strncmp(argv[i], "-m", 2) == 0) {
+ i++;
+ if (i == argc ||
+ ((minVersion = parseMinVersionArg(argv[i])) == 0)) {
+ return showHelpMessage();
+ }
+ }
+ else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "-javaw") == 0) {
doJavaW = true;
}
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"
- );
- return 2;
+ return showHelpMessage();
}
}
// Find the first suitable version of Java we can use.
CPath javaPath;
- int version = findJavaInEnvPath(&javaPath, isJdk);
- if (version < MIN_JAVA_VERSION) {
- version = findJavaInRegistry(&javaPath, isJdk);
+ int version = findJavaInEnvPath(&javaPath, isJdk, minVersion);
+ if (version == 0) {
+ version = findJavaInRegistry(&javaPath, isJdk, minVersion);
}
- if (version < MIN_JAVA_VERSION) {
- version = findJavaInProgramFiles(&javaPath, isJdk);
+ if (version == 0) {
+ version = findJavaInProgramFiles(&javaPath, isJdk, minVersion);
}
- if (version < MIN_JAVA_VERSION || javaPath.isEmpty()) {
+
+ if (version == 0 || javaPath.isEmpty()) {
if (gIsDebug) {
fprintf(stderr, "Failed to find Java on your system.\n");
}
@@ -142,7 +173,7 @@ int main(int argc, char* argv[]) { if (doVersion) {
// Print version found. We already have the version as an integer
// so we don't need to run java -version a second time.
- printf("%d.%d", version / 1000, version % 1000);
+ printf("%d.%d", JAVA_MAJOR(version), JAVA_MINOR(version));
return 0;
}
diff --git a/find_java/src/source/find_java_lib.cpp b/find_java/src/source/find_java_lib.cpp index bf69546..93bc705 100755 --- a/find_java/src/source/find_java_lib.cpp +++ b/find_java/src/source/find_java_lib.cpp @@ -124,13 +124,13 @@ static int checkBinPath(CPath *inOutPath) { }
// Test for the existence of java.exe in a custom path
-int checkJavaInPath(const CPath &path) {
+int checkJavaInPath(const CPath &path, int minVersion) {
SetLastError(0);
int currVersion = 0;
CPath temp(path);
currVersion = checkBinPath(&temp);
- if (currVersion > 0) {
+ if (currVersion > minVersion) {
if (gIsDebug) {
fprintf(stderr, "Java %d found in path: %s\n", currVersion, temp.cstr());
}
@@ -140,7 +140,7 @@ int checkJavaInPath(const CPath &path) { }
// Search java.exe in the environment
-int findJavaInEnvPath(CPath *outJavaPath, bool isJdk) {
+int findJavaInEnvPath(CPath *outJavaPath, bool isJdk, int minVersion) {
SetLastError(0);
int currVersion = 0;
@@ -157,7 +157,7 @@ int findJavaInEnvPath(CPath *outJavaPath, bool isJdk) { }
*outJavaPath = p;
}
- if (currVersion >= MIN_JAVA_VERSION) {
+ if (currVersion >= minVersion) {
// 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;
@@ -180,7 +180,7 @@ int findJavaInEnvPath(CPath *outJavaPath, bool isJdk) { }
int v = checkPath(&p);
- if (v > currVersion) {
+ if (v >= minVersion && v > currVersion) {
if (gIsDebug) {
fprintf(stderr, "Java %d found via env PATH: %s\n", v, p.cstr());
}
@@ -245,7 +245,8 @@ static bool getRegValue(const char *keyPath, // Returns an int which is the version of Java found (e.g. 1006 for 1.6) and the
// matching path in outJavaPath.
// Returns 0 if nothing suitable was found.
-static int exploreJavaRegistry(const char *entry, REGSAM access, CPath *outJavaPath) {
+static int exploreJavaRegistry(const char *entry, REGSAM access, int minVersion,
+ CPath *outJavaPath) {
// Let's visit HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment [CurrentVersion]
CPath rootKey("SOFTWARE\\JavaSoft\\");
@@ -269,7 +270,7 @@ static int exploreJavaRegistry(const char *entry, REGSAM access, CPath *outJavaP }
*outJavaPath = javaHome;
}
- if (versionInt >= MIN_JAVA_VERSION) {
+ if (versionInt >= minVersion) {
// Heuristic: if the current version is good enough, stop here
return versionInt;
}
@@ -328,7 +329,7 @@ static int exploreJavaRegistry(const char *entry, REGSAM access, CPath *outJavaP static bool getMaxJavaInRegistry(const char *entry, REGSAM access, CPath *outJavaPath, int *inOutVersion) {
CPath path;
- int version = exploreJavaRegistry(entry, access, &path);
+ int version = exploreJavaRegistry(entry, access, *inOutVersion, &path);
if (version > *inOutVersion) {
*outJavaPath = path;
*inOutVersion = version;
@@ -337,9 +338,9 @@ static bool getMaxJavaInRegistry(const char *entry, REGSAM access, CPath *outJav return false;
}
-int findJavaInRegistry(CPath *outJavaPath, bool isJdk) {
+int findJavaInRegistry(CPath *outJavaPath, bool isJdk, int minVersion) {
// Check the JRE first, then the JDK.
- int version = MIN_JAVA_VERSION - 1;
+ int version = minVersion - 1; // Inner methods check if they're greater than this version.
bool result = false;
result |= (!isJdk && getMaxJavaInRegistry("Java Runtime Environment", 0, outJavaPath, &version));
result |= getMaxJavaInRegistry("Java Development Kit", 0, outJavaPath, &version);
@@ -403,11 +404,11 @@ static bool checkProgramFiles(CPath *outJavaPath, int *inOutVersion, bool isJdk, return found;
}
-int findJavaInProgramFiles(CPath *outJavaPath, bool isJdk) {
+int findJavaInProgramFiles(CPath *outJavaPath, bool isJdk, int minVersion) {
// Check the C:\Program Files directory
bool result = false;
- int version = MIN_JAVA_VERSION - 1;
+ int version = minVersion - 1; // Inner methods check if they're greater than this version.
result |= checkProgramFiles(outJavaPath, &version, isJdk, false);
// Even if we're 64-bit, try again but check the C:\Program Files (x86) directory, looking for
@@ -421,7 +422,6 @@ int findJavaInProgramFiles(CPath *outJavaPath, bool isJdk) { // --------------
-
// Tries to invoke the java.exe at the given path and extract it's
// version number.
// - outVersionStr: if not null, will capture version as a string (e.g. "1.6")
|