Index: /main/trunk/greenstone2/setup.bat
===================================================================
--- /main/trunk/greenstone2/setup.bat	(revision 31982)
+++ /main/trunk/greenstone2/setup.bat	(revision 31983)
@@ -158,7 +158,95 @@
 @call "!GSDLHOME!\bin\script\checkwritability.bat"
 
+::::::::::::::::::::::::::::::
+if "!GSDL3SRCHOME!" == "" (goto javacheck) else (goto done)
+
+:javacheck
+:: Only for GS2: work out java, and if the bundled jre is found, then set Java env vars with it
+:: Then the same java will be consistently available for all aspects of GS2 (server or GLI, and any subshells these launch)
+echo.
+set MINIMUM_JAVA_VERSION=1.5.0_00
+echo GS2 installation: Checking for Java of version !MINIMUM_JAVA_VERSION! or above
+
+set BUNDLED_JRE=!GSDLHOME!\packages\jre
+if exist "!BUNDLED_JRE!" (
+	set HINT=!BUNDLED_JRE!
+) else (
+	echo No bundled JRE
+	set HINT=
+)
+
+set SEARCH4J_EXECUTABLE=!GSDLHOME!\bin\!GSDLOS!\search4j.exe
+if not exist "!SEARCH4J_EXECUTABLE!" (
+	echo Can't check for java, no Search4j	
+	if not exist "!BUNDLED_JRE!" echo Ensure Java environment variables are set ^(either JAVA_HOME or JRE_HOME and on PATH^) & goto done
+	:: else use Bundled JRE
+	echo Will use the bundled JRE and unset any JAVA_HOME to prevent conflicts
+	set JAVA_HOME=
+	set JRE_HOME=!BUNDLED_JRE!
+	set PATH=!BUNDLED_JRE!\bin;!PATH!
+	goto done
+)
+
+for /F "tokens=*" %%r in ('bin\!GSDLOS!\search4j.exe -p "!HINT!" -m !MINIMUM_JAVA_VERSION!') do set GS_JAVA_HOME=%%r
+
+if "!GS_JAVA_HOME!" == "" (
+	if not exist "!BUNDLED_JRE!" echo There's no bundled JRE.
+	echo setup.bat: Could not find Java in the environment or installation.	
+	echo Set JAVA_HOME or JRE_HOME, and put it on the PATH, if working with Java tools like Lucene.
+	goto done
+)
+
+:: found java, now GS_JAVA_HOME env vars set, set JAVA_HOME else JRE_HOME
+
+if "!GS_JAVA_HOME!" == "!BUNDLED_JRE!" (	
+	:: since our bundled JRE was selected by search4j and we'll be using that, clearing any existing JAVA_HOME to prevent version conflicts	
+	echo Found a bundled JRE. Setting up GS2's Java environment to use this ^(and unsetting any JAVA_HOME to prevent version conflicts^)
+	set JAVA_HOME=
+	set JRE_HOME=!BUNDLED_JRE!
+	set PATH=!BUNDLED_JRE!\bin;!PATH!
+	goto done
+)
+
+:: Otherwise, the java that search4j found is not the bundled jre. In that case
+:: if JAVA_HOME or JRE_HOME is already set to the Java found, then PATH presumably would already be set too.
+if "!JAVA_HOME!" == "!GS_JAVA_HOME!" (
+	echo Looks like the Java environment is already set up with a JAVA_HOME
+	::echo Looks like the Java environment is already set up with a JAVA_HOME, unsetting any JRE_HOME	
+	goto done
+)
+if "!JRE_HOME!" == "!GS_JAVA_HOME!" (
+	echo Looks like the Java environment is already set up with a JRE_HOME, unsetting any JAVA_HOME to prevent java version conflicts
+	set JAVA_HOME=
+	goto done
+)
+
+:: if Java env vars not already set, then set them to the GS_JAVA_HOME found
+echo Found a Java on the system. Setting up GS2's Java environment to use this
+::echo Found a Java on the system. Setting up GS2's Java environment to use this: !GS_JAVA_HOME!
+set PATH=!GS_JAVA_HOME!\bin;!PATH!
+:: extract the last 4 chars from folder name. Could be \jre or jre\ or otherwise
+set javafoldername=!GS_JAVA_HOME:~-4!
+:: now can test if the foldername contains jre or otherwise, and based on that set either JAVA_HOME or JRE_HOME
+:: https://stackoverflow.com/questions/7005951/batch-file-find-if-substring-is-in-string-not-in-a-file
+:: https://ss64.com/nt/syntax-substring.html
+if /i "x!javafoldername:jre=!" == "x!javafoldername!" (
+	echo Setting JAVA_HOME, and unsetting any JRE_HOME to prevent version conflicts
+	set JAVA_HOME=!GS_JAVA_HOME!
+	set JRE_HOME=
+) else (
+	echo Setting JRE_HOME, and unsetting any JAVA_HOME to prevent version conflicts
+	set JRE_HOME=!GS_JAVA_HOME!
+	set JAVA_HOME=
+)
+
+::::::::::::::::::::::::::::::
+
 :done
 popd
-endlocal & set PATH=%PATH%& set GSDLHOME=%GSDLHOME%& set GSDLOS=%GSDLOS%
+endlocal & set PATH=%PATH%& set GSDLHOME=%GSDLHOME%& set GSDLOS=%GSDLOS%& set JRE_HOME=%JRE_HOME%& set JAVA_HOME=%JAVA_HOME%
+
+if not "%JAVA_HOME%" == "" echo JAVA_HOME: %JAVA_HOME%
+if not "%JRE_HOME%" == "" echo JRE_HOME: %JRE_HOME%
+if "%JAVA_HOME%" == "" if "%JRE_HOME%" == "" echo Warning: Neither JAVA_HOME nor JRE_HOME set. Ensure one is set and on PATH.
 
 :: Perl >= v5.18.* randomises map iteration order within a process
