blob: 74f8ac1278cdea06734294dfbde001db500dda69 [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;
68
69 for (int i = 1; i < argc; i++) {
70 if (strncmp(argv[i], "-t", 2) == 0) {
71 testFindJava();
72 return 0;
73
74 } else if (strncmp(argv[i], "-d", 2) == 0) {
75 gIsDebug = true;
76
77 } else if (strncmp(argv[i], "-s", 2) == 0) {
78 doShortPath = true;
79
80 } else if (strncmp(argv[i], "-v", 2) == 0) {
81 doVersion = true;
82
83 } else {
84 printf(
85 "Outputs the path of the first Java.exe found on the local system.\n"
86 "Returns code 0 when found, 1 when not found.\n"
87 "Options:\n"
88 "-h / -help : This help.\n"
89 "-t / -test : Internal test.\n"
90 "-s / -short : Print path in short DOS form.\n"
91 "-v / -version: Only prints the Java version found.\n"
92 );
93 return 2;
94 }
95 }
96
97 CPath javaPath;
98 if (!findJavaInEnvPath(&javaPath) &&
99 !findJavaInRegistry(&javaPath) &&
100 !findJavaInProgramFiles(&javaPath)) {
101 if (gIsDebug) {
102 fprintf(stderr, "Failed to find Java on your system.\n");
103 }
104 return 1;
105 }
106 _ASSERT(!javaPath.isEmpty());
107
108 if (doShortPath) {
109 if (!javaPath.toShortPath(&javaPath)) {
110 fprintf(stderr,
111 "Failed to convert path to a short DOS path: %s\n",
112 javaPath.cstr());
113 return 1;
114 }
115 }
116
117 if (doVersion) {
118 // Print version found
119 CString version;
120 if (getJavaVersion(javaPath, &version)) {
121 printf("%s", version.cstr());
122 return 0;
123 } else {
124 fprintf(stderr, "Failed to get version of %s\n", javaPath.cstr());
125 }
126 }
127
128 // Print java.exe path found
129 printf("%s", javaPath.cstr());
130 return 0;
131}
132
133#endif /* _WIN32 */