aboutsummaryrefslogtreecommitdiffstats
path: root/find_java/src/source/find_java_exe.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'find_java/src/source/find_java_exe.cpp')
-rw-r--r--find_java/src/source/find_java_exe.cpp158
1 files changed, 120 insertions, 38 deletions
diff --git a/find_java/src/source/find_java_exe.cpp b/find_java/src/source/find_java_exe.cpp
index 6b1add9..50ca024 100644
--- a/find_java/src/source/find_java_exe.cpp
+++ b/find_java/src/source/find_java_exe.cpp
@@ -43,21 +43,71 @@
#include <io.h>
#include <fcntl.h>
-static void testFindJava() {
+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"
+ "-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;
+}
+
+static void printError(const char *message) {
+
+ CString error;
+ error.setLastWin32Error(message);
+ printf(error.cstr());
+}
+
+static void testFindJava(bool isJdk, int minVersion) {
CPath javaPath("<not found>");
- int v = findJavaInEnvPath(&javaPath);
- printf("findJavaInEnvPath: [%d] %s\n", v, javaPath.cstr());
+ int v = findJavaInEnvPath(&javaPath, isJdk, minVersion);
+ 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, minVersion);
+ 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, minVersion);
+ printf(" findJavaInProgramFiles [%d] %s\n", v, javaPath.cstr());
+}
+
+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, minVersion);
+
+ printf("\n");
+ printf("Searching for java.exe within a JDK:\n");
+ 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[]) {
@@ -66,12 +116,28 @@ int main(int argc, char* argv[]) {
bool doShortPath = false;
bool doVersion = false;
bool doJavaW = false;
+ 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) {
- testFindJava();
+ testFindJava(minVersion);
return 0;
+ } else if (strncmp(argv[i], "-j", 2) == 0) {
+ isJdk = true;
+
+ } 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;
@@ -81,57 +147,75 @@ 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"
- "-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;
+ }
+ else {
+ return showHelpMessage();
}
}
// Find the first suitable version of Java we can use.
CPath javaPath;
- int version = findJavaInEnvPath(&javaPath);
- if (version < MIN_JAVA_VERSION) {
- version = findJavaInRegistry(&javaPath);
+
+ 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);
}
- if (version < MIN_JAVA_VERSION) {
- version = findJavaInProgramFiles(&javaPath);
+ if (version == 0) {
+ version = findJavaInProgramFiles(&javaPath, isJdk, minVersion);
}
- if (version < MIN_JAVA_VERSION || javaPath.isEmpty()) {
+
+ if (version == 0) {
+ CString s;
+ s.setf("Failed to find Java %d.%d (or newer) on your system. ", JAVA_MAJOR(minVersion),
+ JAVA_MINOR(minVersion));
+
if (gIsDebug) {
- fprintf(stderr, "Failed to find Java on your system.\n");
+ fprintf(stderr, s.cstr());
+ }
+
+ if (shouldPrintError) {
+ printError(s.cstr());
}
+
return 1;
}
_ASSERT(!javaPath.isEmpty());
if (doShortPath) {
- PVOID oldWow64Value = disableWow64FsRedirection();
if (!javaPath.toShortPath(&javaPath)) {
- revertWow64FsRedirection(&oldWow64Value);
- fprintf(stderr,
- "Failed to convert path to a short DOS path: %s\n",
- javaPath.cstr());
+ CString s;
+ s.setf("Failed to convert path (%s) to a short DOS path. ", javaPath.cstr());
+ fprintf(stderr, s.cstr());
+
+ if (shouldPrintError) {
+ printError(s.cstr());
+ }
+
return 1;
}
- revertWow64FsRedirection(&oldWow64Value);
}
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;
}
@@ -140,15 +224,13 @@ int main(int argc, char* argv[]) {
CPath javawPath(javaPath);
javawPath.replaceName("java.exe", "javaw.exe");
// Only accept it if we can actually find the exec
- PVOID oldWow64Value = disableWow64FsRedirection();
if (javawPath.fileExists()) {
javaPath.set(javawPath.cstr());
}
- revertWow64FsRedirection(&oldWow64Value);
}
// Print java.exe path found
- printf("%s", javaPath.cstr());
+ printf(javaPath.cstr());
return 0;
}