I hope someone can give me a little help with this...
BTW, I'm running WinXP/Tomcat. I need to start and end a process on
that machine using a Java servlet that responds to user input. So, I'm
using a method that accepts the command (cmd) and returns the Process
object like this...
public class WindowsExec {
public static Process sendCommand(String cmd) {
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
return proc;
} catch (Throwable t) {
t.printStackTrace();
Process error = null;
return error;
}
}
// I'm not sure if the error handling is done correctly in the above...
// but on to the second part...
public static void killCommand(Process myProcess) {
myProcess.destroy();
}
}
Now I also have an array of Process objects (Part of ServerManager
class) so when I call these methods, I'm using something like this...
server = 0; // this will be dynamically generated
cmd = "C:\\Program Files\\myprog" + server + ".exe";
ServerManager.myProcess[server] = WindowsExec.sendCommand(cmd);
And on a separate JSP page:
WindowsExec.killCommand(ServerManager.myProcess[server]);
The behavior I'm seeing is that the application starts to launch, but
it hangs and I cannot kill the process via the Java code. It seems like
I need a better way to keep track of the Process objects. Any help
appreciated.
dx27s - 11 Jul 2005 20:02 GMT
Okay, after trying a few more things, I realized that my code was
actually calling cmd.exe to launch my other app, so the destroy()
command only killed the cmd.exe process. I fixed this, but I still
don't know why my app is hanging...
For example this code works fine:
WindowsExec.sendCommand(cmd);
But this code will not:
ServerManager.myProcess[server] = WindowsExec.sendCommand(cmd);