Windows SDK: use find_java.bat in all batch wrappers.

SDK Bug: 2315813

Change-Id: If026d09f927b3d0b5707dfd08e1367e4ba2da28b
diff --git a/apkbuilder/etc/apkbuilder.bat b/apkbuilder/etc/apkbuilder.bat
index c4689c6..948eec0 100755
--- a/apkbuilder/etc/apkbuilder.bat
+++ b/apkbuilder/etc/apkbuilder.bat
@@ -24,6 +24,11 @@
 rem issues with directories containing whitespaces.
 cd /d %~dp0
 
+rem Check we have a valid Java.exe in the path.
+set java_exe=
+call find_java.bat
+if not defined java_exe goto :EOF
+
 set jarfile=apkbuilder.jar
 set frameworkdir=
 set libdir=
@@ -40,4 +45,4 @@
 
 set jarpath=%frameworkdir%%jarfile%
 
-call java -Djava.ext.dirs=%frameworkdir% -Djava.library.path=%libdir% -jar %jarpath% %*
+call %java_exe% -Djava.ext.dirs=%frameworkdir% -Djava.library.path=%libdir% -jar %jarpath% %*
diff --git a/ddms/app/etc/ddms.bat b/ddms/app/etc/ddms.bat
index ffe536d..c98621b 100755
--- a/ddms/app/etc/ddms.bat
+++ b/ddms/app/etc/ddms.bat
@@ -24,6 +24,11 @@
 rem issues with directories containing whitespaces.
 cd /d %~dp0
 
+rem Check we have a valid Java.exe in the path.
+set java_exe=
+call find_java.bat
+if not defined java_exe goto :EOF
+
 set jarfile=ddms.jar
 set frameworkdir=
 
@@ -48,7 +53,7 @@
 
 :QueryArch
 
-    for /f %%a in ('java -jar %frameworkdir%archquery.jar') do set swt_path=%frameworkdir%%%a
+    for /f %%a in ('%java_exe% -jar %frameworkdir%archquery.jar') do set swt_path=%frameworkdir%%%a
 
 :SwtDone
 
@@ -60,5 +65,5 @@
 :SetPath
 set javaextdirs=%swt_path%;%frameworkdir%
 
-call java %java_debug% -Djava.ext.dirs=%javaextdirs% -Dcom.android.ddms.bindir= -jar %jarpath% %*
+call %java_exe% %java_debug% -Djava.ext.dirs=%javaextdirs% -Dcom.android.ddms.bindir= -jar %jarpath% %*
 
diff --git a/draw9patch/etc/draw9patch.bat b/draw9patch/etc/draw9patch.bat
index e267b06..c94956f 100755
--- a/draw9patch/etc/draw9patch.bat
+++ b/draw9patch/etc/draw9patch.bat
@@ -24,6 +24,11 @@
 rem issues with directories containing whitespaces.
 cd /d %~dp0
 
+rem Check we have a valid Java.exe in the path.
+set java_exe=
+call find_java.bat
+if not defined java_exe goto :EOF
+
 set jarfile=draw9patch.jar
 set frameworkdir=
 set libdir=
@@ -38,4 +43,4 @@
 
 set jarpath=%frameworkdir%%jarfile%
 
-call java -Djava.ext.dirs=%frameworkdir% -jar %jarpath% %*
+call %java_exe% -Djava.ext.dirs=%frameworkdir% -jar %jarpath% %*
diff --git a/files/find_java.bat b/files/find_java.bat
new file mode 100755
index 0000000..6039e89
--- /dev/null
+++ b/files/find_java.bat
@@ -0,0 +1,80 @@
+@echo off

+rem Copyright (C) 2007 The Android Open Source Project

+rem

+rem Licensed under the Apache License, Version 2.0 (the "License");

+rem you may not use this file except in compliance with the License.

+rem You may obtain a copy of the License at

+rem

+rem      http://www.apache.org/licenses/LICENSE-2.0

+rem

+rem Unless required by applicable law or agreed to in writing, software

+rem distributed under the License is distributed on an "AS IS" BASIS,

+rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+rem See the License for the specific language governing permissions and

+rem limitations under the License.

+

+rem This script is called by the other batch files to find a suitable Java.exe

+rem to use. The script changes the "java_exe" env variable. The variable

+rem is left unset if Java.exe was not found.

+

+rem Useful links:

+rem Command-line reference:

+rem   http://technet.microsoft.com/en-us/library/bb490890.aspx

+

+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

+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 :EOF

+

+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.

diff --git a/hierarchyviewer/etc/hierarchyviewer.bat b/hierarchyviewer/etc/hierarchyviewer.bat
index 2024a79..28a4d8d 100755
--- a/hierarchyviewer/etc/hierarchyviewer.bat
+++ b/hierarchyviewer/etc/hierarchyviewer.bat
@@ -24,6 +24,11 @@
 rem issues with directories containing whitespaces.
 cd /d %~dp0
 
+rem Check we have a valid Java.exe in the path.
+set java_exe=
+call find_java.bat
+if not defined java_exe goto :EOF
+
 set jarfile=hierarchyviewer.jar
 set frameworkdir=
 set libdir=
@@ -38,4 +43,4 @@
 
 set jarpath=%frameworkdir%%jarfile%
 
-call java -Xmx512m -Djava.ext.dirs=%frameworkdir% -Dhierarchyviewer.adb= -jar %jarpath% %*
+call %java_exe% -Xmx512m -Djava.ext.dirs=%frameworkdir% -Dhierarchyviewer.adb= -jar %jarpath% %*
diff --git a/layoutopt/app/etc/layoutopt.bat b/layoutopt/app/etc/layoutopt.bat
index a7e6ade..7fedf31 100755
--- a/layoutopt/app/etc/layoutopt.bat
+++ b/layoutopt/app/etc/layoutopt.bat
@@ -24,6 +24,11 @@
 rem issues with directories containing whitespaces.
 cd /d %~dp0
 
+rem Check we have a valid Java.exe in the path.
+set java_exe=
+call find_java.bat
+if not defined java_exe goto :EOF
+
 set jarfile=layoutopt.jar
 set frameworkdir=
 
@@ -44,5 +49,5 @@
 
 set javaextdirs=%swt_path%;%frameworkdir%
 
-call java %java_debug% -Djava.ext.dirs=%javaextdirs% -jar %jarpath% %*
+call %java_exe% %java_debug% -Djava.ext.dirs=%javaextdirs% -jar %jarpath% %*
 
diff --git a/sdkmanager/app/etc/android.bat b/sdkmanager/app/etc/android.bat
index 782003d..3aeebc2 100755
--- a/sdkmanager/app/etc/android.bat
+++ b/sdkmanager/app/etc/android.bat
@@ -32,13 +32,10 @@
 cd /d %~dp0
 
 
-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
+rem Check we have a valid Java.exe in the path.
+set java_exe=
+call find_java.bat
+if not defined java_exe goto :EOF
 
 set jar_path=lib\sdkmanager.jar
 
@@ -83,55 +80,5 @@
 
 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
diff --git a/traceview/etc/traceview.bat b/traceview/etc/traceview.bat
index 02fbe85..15530a9 100755
--- a/traceview/etc/traceview.bat
+++ b/traceview/etc/traceview.bat
@@ -24,6 +24,11 @@
 rem issues with directories containing whitespaces.
 cd /d %~dp0
 
+rem Check we have a valid Java.exe in the path.
+set java_exe=
+call find_java.bat
+if not defined java_exe goto :EOF
+
 set jarfile=traceview.jar
 set frameworkdir=
 
@@ -43,7 +48,7 @@
 
 :QueryArch
 
-    for /f %%a in ('java -jar %frameworkdir%archquery.jar') do set swt_path=%frameworkdir%%%a
+    for /f %%a in ('%java_exe% -jar %frameworkdir%archquery.jar') do set swt_path=%frameworkdir%%%a
 
 :SwtDone
 
@@ -55,4 +60,4 @@
 :SetPath
 set javaextdirs=%swt_path%;%frameworkdir%
 
-call java -Djava.ext.dirs=%javaextdirs% -Dcom.android.traceview.toolsdir= -jar %jarpath% %*
+call %java_exe% -Djava.ext.dirs=%javaextdirs% -Dcom.android.traceview.toolsdir= -jar %jarpath% %*