blob: c74cfd9eb3a4b03aecd9abfc1933b4ae197f1a2d [file] [log] [blame]
Raphaeld2d69992012-01-24 15:06:56 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * "find_java.exe", for Windows only.
19 * Tries to find a Java binary in a variety of places and prints the
20 * first one found on STDOUT and returns 0.
21 *
22 * If not found, returns error 1 with no message
23 * (unless ANDROID_SDKMAN_DEBUG or -d if set, in which case there's a message on STDERR).
24 *
25 * Implementation details:
26 * - We don't have access to ATL or MFC.
27 * - We don't want to pull in things like STL.
28 * - No Unicode/MBCS support for now.
29 *
30 * TODO for later version:
31 * - provide an env variable to let users override which version is being used.
32 * - if there's more than one java.exe found, enumerate them all.
33 * - and in that case take the one with the highest Java version number.
34 * - since that operation is expensive, do it only once and cache the result
35 * in a temp file. If the temp file is not found or the java binary no
36 * longer exists, re-run the enumaration.
37 */
38
39#ifdef _WIN32
40
41#include "utils.h"
42#include "find_java.h"
43#include <io.h>
44#include <fcntl.h>
45
46static void testFindJava() {
47
48 CPath javaPath("<not found>");
49 bool ok = findJavaInEnvPath(&javaPath);
50 printf("findJavaInEnvPath: [%s] %s\n", ok ? "OK" : "FAIL", javaPath.cstr());
51
52 javaPath.set("<not found>");
53 ok = findJavaInRegistry(&javaPath);
54 printf("findJavaInRegistry [%s] %s\n", ok ? "OK" : "FAIL", javaPath.cstr());
55
56 javaPath.set("<not found>");
57 ok = findJavaInProgramFiles(&javaPath);
58 printf("findJavaInProgramFiles [%s] %s\n", ok ? "OK" : "FAIL", javaPath.cstr());
59}
60
61
62int main(int argc, char* argv[]) {
63
64 gIsConsole = true; // tell utils to to print errors to stderr
65 gIsDebug = (getenv("ANDROID_SDKMAN_DEBUG") != NULL);
66 bool doShortPath = false;
67 bool doVersion = false;
Raphaela418de52012-01-26 19:15:43 -080068 bool doJavaW = false;
Raphaeld2d69992012-01-24 15:06:56 -080069
70 for (int i = 1; i < argc; i++) {
71 if (strncmp(argv[i], "-t", 2) == 0) {
72 testFindJava();
73 return 0;
74
75 } else if (strncmp(argv[i], "-d", 2) == 0) {
76 gIsDebug = true;
77
78 } else if (strncmp(argv[i], "-s", 2) == 0) {
79 doShortPath = true;
80
81 } else if (strncmp(argv[i], "-v", 2) == 0) {
82 doVersion = true;
83
Raphaela418de52012-01-26 19:15:43 -080084 } else if (strcmp(argv[i], "-w") == 0 || strcmp(argv[i], "-javaw") == 0) {
85 doJavaW = true;
86
Raphaeld2d69992012-01-24 15:06:56 -080087 } else {
88 printf(
89 "Outputs the path of the first Java.exe found on the local system.\n"
90 "Returns code 0 when found, 1 when not found.\n"
91 "Options:\n"
92 "-h / -help : This help.\n"
93 "-t / -test : Internal test.\n"
94 "-s / -short : Print path in short DOS form.\n"
Raphaela418de52012-01-26 19:15:43 -080095 "-w / -javaw : Search a matching javaw.exe; defaults to java.exe if not found.\n"
Raphaeld2d69992012-01-24 15:06:56 -080096 "-v / -version: Only prints the Java version found.\n"
97 );
98 return 2;
99 }
100 }
101
102 CPath javaPath;
103 if (!findJavaInEnvPath(&javaPath) &&
104 !findJavaInRegistry(&javaPath) &&
105 !findJavaInProgramFiles(&javaPath)) {
106 if (gIsDebug) {
107 fprintf(stderr, "Failed to find Java on your system.\n");
108 }
109 return 1;
110 }
111 _ASSERT(!javaPath.isEmpty());
112
113 if (doShortPath) {
114 if (!javaPath.toShortPath(&javaPath)) {
115 fprintf(stderr,
116 "Failed to convert path to a short DOS path: %s\n",
117 javaPath.cstr());
118 return 1;
119 }
120 }
121
122 if (doVersion) {
123 // Print version found
124 CString version;
125 if (getJavaVersion(javaPath, &version)) {
126 printf("%s", version.cstr());
127 return 0;
128 } else {
129 fprintf(stderr, "Failed to get version of %s\n", javaPath.cstr());
130 }
131 }
132
Raphaela418de52012-01-26 19:15:43 -0800133 if (doJavaW) {
134 // Try to find a javaw.exe instead of java.exe at the same location.
135 CPath javawPath(javaPath);
136 javawPath.replaceName("java.exe", "javaw.exe");
137 // Only accept it if we can actually find the exec
138 PVOID oldWow64Value = disableWow64FsRedirection();
139 if (javawPath.fileExists()) {
140 javaPath.set(javawPath.cstr());
141 }
142 revertWow64FsRedirection(&oldWow64Value);
143 }
144
Raphaeld2d69992012-01-24 15:06:56 -0800145 // Print java.exe path found
146 printf("%s", javaPath.cstr());
147 return 0;
148}
149
150#endif /* _WIN32 */