Ejecutar un comando de DOS en Background desde java

jaltabo
03 de Marzo del 2005
Necesito ejecutar comandos del DOS, pero sin que se abra una ventana de sesion dentro de windowsXP, por ejemplo si deseo ejecutar un archivo por lotes

Alexis Pardo Aliaga
03 de Marzo del 2005
REVISA ESTE CODIGO, FORMA PARTE DE UNA JSP QUE TENGO PARA REALIZAR BACKUPS DE BD MySQL, revisa el mètodo startProcess:

<%@page import="java.util.*,
java.net.*,
java.text.*,
java.util.zip.*,
java.io.*"
%>
<%!
/**
* Command of the shell interpreter and the parameter to run a programm
*/
private static final String[] COMMAND_INTERPRETER = {"cmd", "/C"}; // Dos,Windows
//private static final String[] COMMAND_INTERPRETER = {"/bin/sh","-c"}; // Unix

/**
* Max time in ms a process is allowed to run, before it will be terminated
*/
private static final long MAX_PROCESS_RUNNING_TIME = 30 * 1000; //30 seconds

//The number of colums for the edit field
private static final int EDITFIELD_COLS = 85;
//The number of rows for the edit field
private static final int EDITFIELD_ROWS = 30;

/**
* Converts some important chars (int) to the corresponding html string
*/
static String conv2Html(int i) {
if (i == '&') return "&amp;";
else if (i == '<') return "&lt;";
else if (i == '>') return "&gt;";
else if (i == '"') return "&quot;";
else return "" + (char) i;
}

/**
* Converts a normal string to a html conform string
*/
static String conv2Html(String st) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < st.length(); i++) {
buf.append(conv2Html(st.charAt(i)));
}
return buf.toString();
}

/**
* Starts a native process on the server
* @param command the command to start the process
* @param dir the dir in which the process starts
*/
static String startProcess(String command, String dir) throws IOException {
StringBuffer ret = new StringBuffer();
String[] comm = new String[3];
comm[0] = COMMAND_INTERPRETER[0];
comm[1] = COMMAND_INTERPRETER[1];
comm[2] = command;
long start = System.currentTimeMillis();
try {
//Start process
Process ls_proc = Runtime.getRuntime().exec(comm, null, new File(dir));
//Get input and error streams
BufferedInputStream ls_in = new BufferedInputStream(ls_proc.getInputStream());
BufferedInputStream ls_err = new BufferedInputStream(ls_proc.getErrorStream());

boolean end = false;
while (!end) {
int c = 0;
while ((ls_err.available() > 0) && (++c <= 1000)) {
ret.append(conv2Html(ls_err.read()));
}
c = 0;
while ((ls_in.available() > 0) && (++c <= 1000)) {
ret.append(conv2Html(ls_in.read()));
}
try {
ls_proc.exitValue();
//if the process has not finished, an exception is thrown
//else
while (ls_err.available() > 0)
ret.append(conv2Html(ls_err.read()));
while (ls_in.available() > 0)
ret.append(conv2Html(ls_in.read()));
end = true;
}
catch (IllegalThreadStateException ex) {
//Process is running
}
//The process is not allowed to run longer than given time.
if (System.currentTimeMillis() - start > MAX_PROCESS_RUNNING_TIME) {
ls_proc.destroy();
end = true;
ret.append("!!!! Process has timed out, destroyed !!!!!");
}
try {
Thread.sleep(50);
}
catch (InterruptedException ie) {}
}
}
catch (IOException e) {
ret.append("Error: " + e);
}
return ret.toString();
}
%>

<html>
<head>
<title>Backup de la Base de Datos</title>
</head>
<body>

<FORM METHOD=POST ACTION="backup1.jsp">
<input type="hidden" name="dir" value="C:mysqlbin">
<input size="85" type="text" name="command" value="">
<tr><td><input class="boton" type="Submit" name="Submit" value="Launch">
<input type="hidden" name="sort" value="1">
<input class="boton" type="Submit" name="Submit" value="Cancel"></td></tr>
</FORM>

<%
if(request.getParameter("command")!=null) {
%>
<%
out.println("<textarea name="text" wrap="off" cols="" + EDITFIELD_COLS
+ "" rows="" + EDITFIELD_ROWS + "" readonly>");
%>
<%
String rt;
rt = startProcess(request.getParameter("command"), request.getParameter("dir"));
out.println(rt);
%>
</textarea>

<%
}
else {
%>
No hay nada para ejecutar.
<%
}
%>

</body>
</html>