blob: db1f949851ee58fc0138c3a5fa15b750618f54a9 [file] [log] [blame]
Raphael047e5f52010-01-26 16:08:17 -08001@echo off
2rem Copyright (C) 2007 The Android Open Source Project
3rem
4rem Licensed under the Apache License, Version 2.0 (the "License");
5rem you may not use this file except in compliance with the License.
6rem You may obtain a copy of the License at
7rem
8rem http://www.apache.org/licenses/LICENSE-2.0
9rem
10rem Unless required by applicable law or agreed to in writing, software
11rem distributed under the License is distributed on an "AS IS" BASIS,
12rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13rem See the License for the specific language governing permissions and
14rem limitations under the License.
15
16rem This script is called by the other batch files to find a suitable Java.exe
17rem to use. The script changes the "java_exe" env variable. The variable
18rem is left unset if Java.exe was not found.
19
20rem Useful links:
21rem Command-line reference:
22rem http://technet.microsoft.com/en-us/library/bb490890.aspx
23
24rem Check we have a valid Java.exe in the path. The return code will
25rem be 0 if the command worked or 9009 if the exec failed (program not found).
26rem Java itself will return 1 if the argument is not understood.
Raphael58fd5072011-11-11 10:43:03 -080027set java_exe=java.exe
28rem search it in the path and verify we can execute it
29for %%a in (%java_exe%) do set java_exe=%%~s$PATH:a
30if not exist %java_exe% goto SearchForJava
Raphael047e5f52010-01-26 16:08:17 -080031%java_exe% -version 2>nul
32if ERRORLEVEL 1 goto SearchForJava
Raphael58fd5072011-11-11 10:43:03 -080033goto :SearchJavaW
Raphael047e5f52010-01-26 16:08:17 -080034
35
36rem ---------------
37:SearchForJava
38rem We get here if the default %java_exe% was not found in the path.
39rem Search for an alternative in %ProgramFiles%\Java\*\bin\java.exe
40
41echo.
Raphaelea66c922010-01-27 12:48:34 -080042echo WARNING: Java not found in your path.
Raphael Molla5dd8652010-08-20 21:54:59 -070043
Raphael Moll11583cc2011-08-26 14:13:16 -070044rem The strategy is to look for Java under these 3 locations:
45rem - %ProgramFiles%, which may point to either a 32-bit or 64-bit install
46rem depending on the current invocation context
47rem - %ProgramW6432%, which points to a 32-bit install. This may not be defined.
48rem - %ProgramFiles(x86)%, which points to a 64-bit install. This may not be defined.
49
50if not defined ProgramFiles goto :Check64
51echo Checking if Java is installed in %ProgramFiles%\Java.
Raphael Molla5dd8652010-08-20 21:54:59 -070052
53set java_exe=
54for /D %%a in ( "%ProgramW6432%\Java\*" ) do call :TestJavaDir "%%a"
Raphael58fd5072011-11-11 10:43:03 -080055if defined java_exe goto :SearchJavaW
Raphael Molla5dd8652010-08-20 21:54:59 -070056
Raphael Moll11583cc2011-08-26 14:13:16 -070057rem Check for the "default" 64-bit version if it's not the same path
58:Check64
59if not defined ProgramW6432 goto :Check32
60if "%ProgramW6432%"=="%ProgramFiles%" goto :Check32
61echo Checking if Java is installed in %ProgramW6432%\Java instead (64-bit).
Raphael047e5f52010-01-26 16:08:17 -080062
63set java_exe=
Raphael Moll11583cc2011-08-26 14:13:16 -070064for /D %%a in ( "%ProgramW6432%\Java\*" ) do call :TestJavaDir "%%a"
Raphael58fd5072011-11-11 10:43:03 -080065if defined java_exe goto :SearchJavaW
Raphael047e5f52010-01-26 16:08:17 -080066
Raphael Moll11583cc2011-08-26 14:13:16 -070067rem Check for the "default" 32-bit version if it's not the same path
68:Check32
69if not defined ProgramFiles(x86) goto :CheckFailed
70if "%ProgramFiles(x86)%"=="%ProgramFiles%" goto :CheckFailed
71echo Checking if Java is installed in %ProgramFiles(x86)%\Java instead (32-bit).
72
73set java_exe=
74for /D %%a in ( "%ProgramFiles(x86)%\Java\*" ) do call :TestJavaDir "%%a"
Raphael58fd5072011-11-11 10:43:03 -080075if defined java_exe goto :SearchJavaW
Raphael Moll11583cc2011-08-26 14:13:16 -070076
77:CheckFailed
Raphael047e5f52010-01-26 16:08:17 -080078echo.
Raphaelea66c922010-01-27 12:48:34 -080079echo ERROR: No suitable Java found. In order to properly use the Android Developer
Raphael Molla5dd8652010-08-20 21:54:59 -070080echo Tools, you need a suitable version of Java JDK installed on your system.
81echo We recommend that you install the JDK version of JavaSE, available here:
82echo http://www.oracle.com/technetwork/java/javase/downloads
Raphael047e5f52010-01-26 16:08:17 -080083echo.
84echo You can find the complete Android SDK requirements here:
85echo http://developer.android.com/sdk/requirements.html
86echo.
87goto :EOF
88
89rem ---------------
90:TestJavaDir
91rem This is a "subrountine" for the for /D above. It tests the short version
92rem of the %1 path (i.e. the path with only short names and no spaces).
93rem However we use the full version without quotes (e.g. %~1) for pretty print.
94if defined java_exe goto :EOF
95set full_path=%~1\bin\java.exe
96set short_path=%~s1\bin\java.exe
Raphael047e5f52010-01-26 16:08:17 -080097
98%short_path% -version 2>nul
99if ERRORLEVEL 1 goto :EOF
100set java_exe=%short_path%
101
102echo.
103echo Java was found at %full_path%.
104echo Please consider adding it to your path:
105echo - Under Windows XP, open Control Panel / System / Advanced / Environment Variables
Raphael Molla5dd8652010-08-20 21:54:59 -0700106echo - Under Windows Vista or Windows 7, open Control Panel / System / Advanced System Settings / Environment Variables
Raphael047e5f52010-01-26 16:08:17 -0800107echo At the end of the "Path" entry in "User variables", add the following:
108echo ;%full_path%
109echo.
Raphael58fd5072011-11-11 10:43:03 -0800110goto :EOF
111
112rem ---------------
113:SearchJavaW
114rem Called once java_exe has been set. Try to see if we can find a javaw
115rem to use. If not, we'll default to using java_exe.
116for %%a in (%java_exe%) do set p=%%~pa
117for %%a in (%java_exe%) do set n=%%~na
118for %%a in (%java_exe%) do set x=%%~xa
119set n=%n:java=javaw%
120set javaw_exe=%p%%n%%x%
121if not exist %javaw_exe% set javaw_exe=%java_exe%
122goto :EOF