android.bat now checks if Java is installed.

When not in the path, search for it in the program files
where java installs by default.
If not found, let the user know where to download it.

SDK Bug: 2315813

Change-Id: I4b077606d86969f989279a5eaeebb2f1895a6ef2
diff --git a/sdkmanager/app/etc/android.bat b/sdkmanager/app/etc/android.bat
index 58790ce..782003d 100755
--- a/sdkmanager/app/etc/android.bat
+++ b/sdkmanager/app/etc/android.bat
@@ -13,6 +13,10 @@
 rem See the License for the specific language governing permissions and
 rem limitations under the License.
 
+rem Useful links:
+rem Command-line reference:
+rem   http://technet.microsoft.com/en-us/library/bb490890.aspx
+
 rem don't modify the caller's environment
 setlocal
 
@@ -21,16 +25,26 @@
 set prog=%~f0
 
 rem Grab current directory before we change it
-set workdir=%cd%
+set work_dir=%cd%
 
 rem Change current directory and drive to where the script is, to avoid
 rem issues with directories containing whitespaces.
 cd /d %~dp0
 
-set jarpath=lib\sdkmanager.jar
+
+rem Check we have a valid Java.exe in the path. The return code will
+rem be 0 if the command worked or 9009 if the exec failed (program not found).
+rem Java itself will return 1 if the argument is not understood.
+set java_exe=java
+%java_exe% -version 2>nul
+if ERRORLEVEL 1 goto SearchForJava
+:JavaFound
+
+set jar_path=lib\sdkmanager.jar
 
 rem Set SWT.Jar path based on current architecture (x86 or x86_64)
-for /f %%a in ('java -jar lib\archquery.jar') do set swt_path=lib\%%a
+for /f %%a in ('%java_exe% -jar lib\archquery.jar') do set swt_path=lib\%%a
+
 
 if "%1 %2"=="update sdk" goto StartUi
 if not "%1"=="" goto EndTempCopy
@@ -43,16 +57,16 @@
     rem arguments, to display the SDK Updater UI. This allows the updater to
     rem update the tools directory where the updater itself is located.
 
-    set tmpdir=%TEMP%\temp-android-tool
-    xcopy lib\x86 %tmpdir%\lib\x86 /I /E /C /G /R /Y /Q > nul
-    copy /B /D /Y lib\androidprefs.jar   %tmpdir%\lib\       > nul
-    copy /B /D /Y lib\org.eclipse.*      %tmpdir%\lib\       > nul
-    copy /B /D /Y lib\sdk*               %tmpdir%\lib\       > nul
-    copy /B /D /Y lib\commons-compress*  %tmpdir%\lib\       > nul
+    set tmp_dir=%TEMP%\temp-android-tool
+    xcopy lib\x86 %tmp_dir%\lib\x86 /I /E /C /G /R /Y /Q > nul
+    copy /B /D /Y lib\androidprefs.jar   %tmp_dir%\lib\       > nul
+    copy /B /D /Y lib\org.eclipse.*      %tmp_dir%\lib\       > nul
+    copy /B /D /Y lib\sdk*               %tmp_dir%\lib\       > nul
+    copy /B /D /Y lib\commons-compress*  %tmp_dir%\lib\       > nul
 
-    rem jarpath and swt_path are relative to PWD so we don't need to adjust them, just change dirs.
-    set toolsdir=%cd%
-    cd %tmpdir%
+    rem jar_path and swt_path are relative to PWD so we don't need to adjust them, just change dirs.
+    set tools_dir=%cd%
+    cd %tmp_dir%
 
 :EndTempCopy
     
@@ -65,6 +79,59 @@
     exit /B
 
 :SetPath
-set javaextdirs=%swt_path%;lib\
+set java_ext_dirs=%swt_path%;lib\
 
-call java -Djava.ext.dirs=%javaextdirs% -Dcom.android.sdkmanager.toolsdir="%toolsdir%" -Dcom.android.sdkmanager.workdir="%workdir%" -jar %jarpath% %*
+rem Finally exec the java program and end here.
+call %java_exe% -Djava.ext.dirs=%java_ext_dirs% -Dcom.android.sdkmanager.toolsdir="%tools_dir%" -Dcom.android.sdkmanager.workdir="%work_dir%" -jar %jar_path% %*
+goto :EOF
+
+rem ---------------
+:SearchForJava
+rem We get here if the default %java_exe% was not found in the path.
+rem Search for an alternative in %ProgramFiles%\Java\*\bin\java.exe
+
+echo.
+echo Java not found in your path.
+echo Checking it it's installed in %ProgramFiles%\Java instead.
+echo.
+
+set java_exe=
+for /D %%a in ( "%ProgramFiles%\Java\*" ) do call :TestJavaDir "%%a"
+if defined java_exe goto JavaFound
+
+echo.
+echo No suitable Java found. In order to properly use the Android Developer Tools,
+echo you need a suitable version of Java installed on your system. We recommend
+echo that you install the JDK version of JavaSE, available here:
+echo   http://java.sun.com/javase/downloads/
+echo.
+echo You can find the complete Android SDK requirements here:
+echo   http://developer.android.com/sdk/requirements.html
+echo.
+goto :EOF
+
+rem ---------------
+:TestJavaDir
+rem This is a "subrountine" for the for /D above. It tests the short version
+rem of the %1 path (i.e. the path with only short names and no spaces).
+rem However we use the full version without quotes (e.g. %~1) for pretty print.
+if defined java_exe goto :EOF
+set full_path=%~1\bin\java.exe
+set short_path=%~s1\bin\java.exe
+rem [for debugging] echo Testing %full_path%
+
+%short_path% -version 2>nul
+if ERRORLEVEL 1 goto :EOF
+set java_exe=%short_path%
+
+echo.
+echo Java was found at %full_path%.
+echo Please consider adding it to your path:
+echo - Under Windows XP, open Control Panel / System / Advanced / Environment Variables
+echo - Under Windows Vista, open Control Panel / System / Advanced System Settings
+echo                                                    / Environment Variables
+echo At the end of the "Path" entry in "User variables", add the following:
+echo   ;%full_path%
+echo.
+
+rem EOF