Index: main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java
===================================================================
--- main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java	(revision 31582)
+++ main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java	(revision 31583)
@@ -766,26 +766,5 @@
 		    //thread.start();
 
-		    try {
-			String shellCommand = null;
-			Process p = null;
-			if (Utility.isWindows()) {
-				// cmd /C "cd "C:\path\to\greenstone3" && ant stop"
-			    p = Runtime.getRuntime().exec("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");	
-			} else {
-			    p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
-			}
-			// doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6 
-			// (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine.
-			/*if(p != null && p.waitFor() == 0) {
-			    DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
-			}
-			else {
-			    System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
-			    //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
-			}*/
-		    } catch(Exception e) {
-			System.err.println("Exception when trying to stop the tomcat web server: " + e);
-			DebugStream.printStackTrace(e);
-		    }
+		    GS3ServerThread.stopServer();
 		    
 		}
Index: main/trunk/gli/src/org/greenstone/gatherer/util/GS3ServerThread.java
===================================================================
--- main/trunk/gli/src/org/greenstone/gatherer/util/GS3ServerThread.java	(revision 31582)
+++ main/trunk/gli/src/org/greenstone/gatherer/util/GS3ServerThread.java	(revision 31583)
@@ -37,4 +37,8 @@
 
 package org.greenstone.gatherer.util;
+
+import org.greenstone.gatherer.Configuration;
+import org.greenstone.gatherer.DebugStream;
+
 import java.io.File;
 
@@ -51,5 +55,5 @@
     }
     
-    public void run()
+    public void old_run()
     {
 	try
@@ -87,5 +91,5 @@
 			// (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
 			SafeProcess.OutputStreamGobbler inputGobbler
-			    = new SafeProcess.OutputStreamGobbler(p.getOutputStream(), null);			
+			    = new SafeProcess.OutputStreamGobbler(p.getOutputStream());			
 			SafeProcess.InputStreamGobbler errorGobbler
 			    = new SafeProcess.InputStreamGobbler(p.getErrorStream(), true);
@@ -116,3 +120,120 @@
 	    }
     }
+
+    public void run()
+    {
+
+	///System.err.println("**** GS3 server : " + _ant_command);
+
+	SafeProcess p = null;
+	if (Utility.isWindows()) {
+	    if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start
+		_ant_command = "start";
+	    }
+	    
+	    // The path in quotes, and the entire sequence of commands in quotes as well
+	    // E.g. the following works in a Runtime.exec() call:
+	    // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
+	    // and it preserves any spaces in the path to GSDL3SRCHOME (_gsdl3_src_path).
+	    p = new SafeProcess("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\"");
+	}
+	else {
+	    if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart
+		_ant_command = "restart";
+	    }
+	    p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""});
+	}
+	
+	
+	// in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries)
+	// need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary
+	// and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start
+	// after the user has quit GLI.
+	// Process takes no input, but we will still catch the process' instream too
+	// And we'll catch the error and output streams to prevent them from blocking during waitFor()
+	// (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
+
+
+	// prepare our SafeProcess object
+	p.setSplitStdErrorNewLines(true);
+
+	// run it
+	int result = p.runProcess(); // uses default process streamgobbler behaviours and
+	// does the important part: waitFor() the process (ant stop or start or re-start) to terminate.
+	// The int result returned is the exitvalue upon Process.waitFor() returning
+	
+	if(result != 0) {
+	    System.err.println("**** Failed to successfully " + _ant_command + " the GS3 server.");
+	}
+	///else {
+	///System.err.println("**** " + _ant_command + " of the GS3 server successful.");
+	///}
+	
+    }
+
+    public static void old_stopServer() {
+
+	System.err.println("Stopping GS3 Server");
+	try {
+	    String shellCommand = null;
+	    Process p = null;
+	    if (Utility.isWindows()) {
+		// cmd /C "cd "C:\path\to\greenstone3" && ant stop"
+		p = Runtime.getRuntime().exec("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");	
+	    } else {
+		p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
+	    }
+	    // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6 
+	    // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine.
+	    /*if(p != null && p.waitFor() == 0) {
+	      DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
+	      }
+	      else {
+	      System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
+	      //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
+	      }*/
+	} catch(Exception e) {
+	    System.err.println("Exception when trying to stop the tomcat web server: " + e);
+	    DebugStream.printStackTrace(e);
+	}
+    }
+
+    // can't call ant stop from its own thread - what if GLI has exited by then?
+    // issue call to ant stop from the main GLI thread
+    //GS3ServerThread thread = new GS3ServerThread(Configuration.gsdl_path, "stop");
+    //thread.start();
+    // So, static function to issue the command to stop the server from GLI's own thread.
+    // This will block the main GLI thread until the server has stopped.
+    public static void stopServer() {
+
+	SafeProcess p = null;
+	if (Utility.isWindows()) {
+	    // cmd /C "cd "C:\path\to\greenstone3" && ant stop"
+	    p = new SafeProcess("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");	
+	} else {
+	    p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""});
+	}
+
+	System.err.println("Issuing stop command to GS3 Server. Waiting for GS3 server to stop...");
+	int result = p.runProcess();
+	if(result == 0) {
+	    System.err.println("Successfully stopped GS3 server.");
+	    //DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
+	}
+	else {
+	    System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
+	    //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
+	}
+	
+	// doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6 
+	// (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine.
+	/*if(p != null && p.waitFor() == 0) {
+	  DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT");
+	  }
+	  else {
+	  System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT");
+	  //throw new Exception ("Failed to successfully stop the GS3 server on exit.");
+	  }*/
+	
+    }
 }
Index: main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java
===================================================================
--- main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java	(revision 31582)
+++ main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java	(revision 31583)
@@ -23,4 +23,5 @@
     //static Logger logger = Logger.getLogger(org.greenstone.util.SafeProcess.class.getName());
 
+    private String command = null;
     private String[] command_args = null;
     private String[] envp = null;
@@ -51,4 +52,9 @@
     }
 
+    public SafeProcess(String cmdStr)
+    {
+	command = cmdStr;
+    }
+
     // cmd args with env version, launchDir can be null.
     public SafeProcess(String[] cmd_args, String[] envparams, File launchDir)
@@ -96,5 +102,5 @@
 
 //***************** Copied from gli's gui/FormatConversionDialog.java *************//
-    public void runProcess() {
+    public int runProcess() {
 
 	Process prcs = null;
@@ -105,19 +111,24 @@
 	try {	    
 	    Runtime rt = Runtime.getRuntime();	    
-	    
-	    // http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java
-	    //System.err.println("Running process: " + Arrays.toString(command_args));
-
-	    if(this.envp == null) {
-		prcs = rt.exec(this.command_args);
-	    } else { // launch process using cmd str with env params		
-
-		if(this.dir == null) {
-		    //System.err.println("\twith: " + Arrays.toString(this.envp));
-		    prcs = rt.exec(this.command_args, this.envp);
-		} else {
-		    //System.err.println("\tfrom directory: " + this.dir);
-		    //System.err.println("\twith: " + Arrays.toString(this.envp));
-		    prcs = rt.exec(this.command_args, this.envp, this.dir);
+	    if(this.command != null) {
+		prcs = rt.exec(this.command);
+	    }
+	    else { // at least command_args must be set now
+
+		// http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java
+		///System.err.println("SafeProcess running: " + Arrays.toString(command_args));
+
+		if(this.envp == null) { 
+		    prcs = rt.exec(this.command_args);
+		} else { // launch process using cmd str with env params		
+		    
+		    if(this.dir == null) {
+			System.err.println("\twith: " + Arrays.toString(this.envp));
+			prcs = rt.exec(this.command_args, this.envp);
+		    } else {
+			System.err.println("\tfrom directory: " + this.dir);
+			System.err.println("\twith: " + Arrays.toString(this.envp));
+			prcs = rt.exec(this.command_args, this.envp, this.dir);
+		    }
 		}
 	    }
@@ -219,4 +230,5 @@
 	}
 
+	return this.exitValue;
     }
     
@@ -321,4 +333,8 @@
     String inputstr = "";
     ExceptionHandler exceptionHandler = null;
+
+    public OutputStreamGobbler(OutputStream os) {
+	this.os = os;
+    }
 
     public OutputStreamGobbler(OutputStream os, String inputstr)
