Can i execute a bat file from a Servlet?
when i run a bat file from a servlet to run a process on my server cmd.exe starts but seems to hang therein.......
though when i run the bat by double-clicking on the File Icon it executes with no issues.....
I have tried these alternatives with no success.....
any alternatives to this...............?
Runtime r = Runtime.getRuntime();
Process p =null;
try
{
//p = r.exec("E:\\Site\\FileTransfer\\FileTransfer.bat");
//p = r.exec("rundll32 url.dll,FileProtocolHandler " +"E:\\Site\\FileTransfer\\Upload\\FileTransfer.bat");
String[] cmd ={"cmd","/c","E:\\Site\\FileTransfer\\Upload\\FileTransfer.bat"};
p = r.exec(cmd);
}
catch(Exception e)
{
System.out.println("Exception in Runtime batch processing."+e);
}
[1232 byte] By [
chetana] at [2007-10-2 5:38:17]

You need to handle the output buffers
private String executeCommand(SystemCommand sc)
throws Exception {
String result = "";
Process proc = null;
try {
proc = Runtime.getRuntime().exec(sc.getCommand());
result += StreamUtil.getDataFromInputStream(proc.getInputStream());
result += StreamUtil.getDataFromInputStream(proc.getErrorStream());
int code = proc.waitFor();
} catch (Exception e) {
String message =
"SystemCommandRequestQueueThread.executeCommand : " +
e.getMessage();
logger.error(message, e);
throw new Exception(message, e);
} finally {
if (proc != null) {
if (proc.getErrorStream() != null) {
try { proc.getInputStream().close(); } catch (Exception ee) { }
try { proc.getErrorStream().close(); } catch (Exception ee) { }
}
}
}
return result;
}
and StreamUtil:
import java.io.*;
/**
*
*/
public class StreamUtil {
/** Creates a new instance of StreamUtil */
private StreamUtil() {
}
public static String getDataFromInputStream(InputStream is)
throws Exception {
BufferedReader br = null;
String data = "";
StringBuffer sb = new StringBuffer(data);
try {
br =new BufferedReader(new InputStreamReader(is));
while((data = br.readLine())!=null){
sb.append(data);
sb.append(System.getProperty("line.separator"));
}
} catch (Exception e) {
String message =
"StreamUtil.getDataFromInputStream : " +
e.getMessage();
throw new Exception( message, e);
} finally {
if (br != null) {
try { br.close(); } catch (Exception ee) { }
}
}
return sb.toString();
}
}