Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2005

Tip: Looking for answers? Try searching our database.

Starting and ending a process via Java/JSP

Thread view: 
dx27s@yahoo.com - 11 Jul 2005 19:24 GMT
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);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.