New -path arg for find_java

Previously, the only way to specify a custom java.exe location for
find_java was through JAVA_HOME, but now we have one more hook.
If you pass in a custom path, and it is found, the rest of
find_java will be skipped, and that path will be used.

This has a few advantages:
1) Passing in an explicit custom path is really intuitive.

2) JAVA_HOME may be used by other applications, and asking users
to override it for us could cause compatibility issues.

3) This makes life much easier for our installer, which does not
want to blindly overwrite the user's JAVA_HOME environment
variable.

4) If we ever change where we store a java path (new registry, in
a file somewhere), we don't have to recompile find_java.exe to
account for it. All we have to do is update find_java.bat.

5) This gives us a really easy way to tell users who run into
find_java bugs a way to fix them.

Change-Id: I1d7bd95c9225f34c22d987005f32af256f81d908
diff --git a/find_java/src/source/find_java.h b/find_java/src/source/find_java.h
index fe7da4d..49b5fd9 100755
--- a/find_java/src/source/find_java.h
+++ b/find_java/src/source/find_java.h
@@ -30,7 +30,8 @@
 #define MIN_JAVA_VERSION_MINOR 6

 #define MIN_JAVA_VERSION TO_JAVA_VERSION(MIN_JAVA_VERSION_MAJOR, MIN_JAVA_VERSION_MINOR)

 

-int checkJavaInPath(const CPath &path, bool isJdk = false, int minVersion = MIN_JAVA_VERSION);

+int findJavaInPath(const CPath &path, CPath *outJavaPath, 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,

diff --git a/find_java/src/source/find_java_exe.cpp b/find_java/src/source/find_java_exe.cpp
index e311646..50ca024 100644
--- a/find_java/src/source/find_java_exe.cpp
+++ b/find_java/src/source/find_java_exe.cpp
@@ -48,13 +48,14 @@
         "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"

-        "-e / -error  : Print an error message to the console if Java.exe isn't found.\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"

+        "-h / -help       : This help.\n"

+        "-t / -test       : Internal test.\n"

+        "-e / -error      : Print an error message to the console if Java.exe isn't found.\n"

+        "-j / -jdk        : Only returns java.exe found in a JDK.\n"

+        "-s / -short      : Print path in short DOS form.\n"

+        "-p / -path `dir` : A custom path to search first. Pass in JDK base dir if -j is set.\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;

@@ -118,6 +119,7 @@
     bool isJdk = false;

     bool shouldPrintError = false;

     int minVersion = MIN_JAVA_VERSION;

+    const char *customPathStr = NULL;

 

     for (int i = 1; i < argc; i++) {

         if (strncmp(argv[i], "-t", 2) == 0) {

@@ -130,6 +132,12 @@
         } else if (strncmp(argv[i], "-e", 2) == 0) {

             shouldPrintError = true;

 

+        } else if (strncmp(argv[i], "-p", 2) == 0) {

+            i++;

+            if (i == argc) {

+                return showHelpMessage();

+            }

+            customPathStr = argv[i];

         } else if (strncmp(argv[i], "-d", 2) == 0) {

             gIsDebug = true;

 

@@ -157,7 +165,15 @@
 

     // Find the first suitable version of Java we can use.

     CPath javaPath;

-    int version = findJavaInEnvPath(&javaPath, isJdk, minVersion);

+

+    int version = 0;

+    if (customPathStr != NULL) {

+        CPath customPath(customPathStr);

+        version = findJavaInPath(customPath, &javaPath, isJdk, minVersion);

+    }

+    if (version == 0) {

+        version = findJavaInEnvPath(&javaPath, isJdk, minVersion);

+    }

     if (version == 0) {

         version = findJavaInRegistry(&javaPath, isJdk, minVersion);

     }

diff --git a/find_java/src/source/find_java_lib.cpp b/find_java/src/source/find_java_lib.cpp
index 93bc705..134c38b 100755
--- a/find_java/src/source/find_java_lib.cpp
+++ b/find_java/src/source/find_java_lib.cpp
@@ -124,19 +124,28 @@
 }

 

 // Test for the existence of java.exe in a custom path

-int checkJavaInPath(const CPath &path, int minVersion) {

+int findJavaInPath(const CPath &path, CPath *outJavaPath, bool isJdk, int minVersion) {

     SetLastError(0);

 

-    int currVersion = 0;

+    int version = 0;

     CPath temp(path);

-    currVersion = checkBinPath(&temp);

-    if (currVersion > minVersion) {

-        if (gIsDebug) {

-            fprintf(stderr, "Java %d found in path: %s\n", currVersion, temp.cstr());

+    if (!isJdk) {

+        version = checkPath(&temp);

+    }

+    else {

+        if (isJdkPath(temp)) {

+            version = checkBinPath(&temp);

         }

     }

 

-    return currVersion;

+    if (version >= minVersion) {

+        if (gIsDebug) {

+            fprintf(stderr, "Java %d found in path: %s\n", version, temp.cstr());

+        }

+        *outJavaPath = temp;

+        return version;

+    }

+    return 0;

 }

 

 // Search java.exe in the environment